初始化node项目
在命令行输入:
npm init
安装express
在命令行输入:
npm install express
创建server.js文件
const express = require('express')
const path = require('path')
const app = express()
app.use(express.static(path.join(__dirname, 'public')))
app.listen(8080, () => {
console.log('listening port 8080')
})
将要访问的html文件放在public目录里
index.html内容如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>hello</title>
</head>
<body>
<div>Hello world</div>
</body>
</html>
- 在命令行输入:
node server.js
- 访问地址:http://localhost:8080/index.html。
- 打包资源下载:Nodejs simply displays html static pages