likes
comments
collection
share

node + express搭建简易环境访问html

作者站长头像
站长
· 阅读数 26

安装express框架

npm i express -S

新建server.js文件

 var express = require("express");
 var app = express();
// 静态文件
 app.use(express.static('public'))
 
// app.get可以新建多个,如:/two、 /three...等多个页面
 app.get('/',(req,res) => {
     res.sendFile(__dirname+"/"+"index.html") 
 });
 
 //设置端口3000访问地址,即http://localhost:3000
 var server = app.listen(3000, ()=>{
     var port = server.address().port
     console.log("访问地址http://localhost://", port)
 })

运行

node server.js

访问地址为:http://localhost:3000