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↓