In just three minutes, you can build a static web server with nodejs (configure the static web access directory)

Node.js Chinese website Node.js is a JavaScript runtime environment based on Chrome V8 engine. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient. npm, the package manager for Node.js, is the world's largest ecosystem of open source libraries. http://nodejs.cn/ install nodejs first

Create app.js in the root directory of your own website project folder

/**
 【运行起来说明】在当前目录CMD

 cnpm i express & cnpm i forever -g & forever start app.js

 * */
let express = require("express"), app = express();

// 可以同时设置多个别名访问路径:
// app.use("/upload", express.static(__dirname + "/upload"));//配置静态资源文件目录路径(用于外部访问http://127.0.0.1/upload/资源文件)
// app.use("/", express.static(__dirname + "/"));//配置静态网页访问目录【前端代码就放这个文件夹】(用于外部访问http://127.0.0.1/)
// 上面的写法等同于↓
app.use(express.static("./"));

const port = 8080;
app.listen(port, () => console.log(`网站服务器启动,访问地址:http://127.0.0.1:${port}`));

 

The above prompt appears, it means that you can access, so you can use

http://127.0.0.1:8080http://127.0.0.1:8080/

http://localhost:8080http://localhost:8080/

Come to visit the website, if the local IP is bound to the external domain name, you can also use the domain name to visit the website

Tips: In fact, node app.js can also run the program, but the program process hangs after closing the cmd window. Use forever start app.js to continue running the background program when the cmd window is closed (recommended forever)

However, some students hope that the port number 8080 is not behind. What should I do if I want to use port 80, the local port 80 is occupied again.

Click the link below↓

Kill the process occupied by the local port 80 and return you a peaceful paradise The process is dead. Let’s have fun~ Oh yes, don’t forget to follow me with Yijian Sanlian~ https://szq.blog.csdn.net/article/details/123562135

Guess you like

Origin blog.csdn.net/qq_37860634/article/details/123561060