node 单页应用服务 常用工具
快速搭建一个node服务,可以用于检查自己的单页应用是否有问题
app.js
var exppress = require("express");
const fs = require("fs");
var app = exppress();
app.use(exppress.static("dist"));
const port = 3000;
app.listen(port, () => {
console.log(`服务已经启动 at http://localhost:${port}`);
});
//服务端端路由
app.use(function (req, res, next) {
//每次返回index.html
fs.readFile(__dirname + "/dist/index.html", function (err, data) {
if (err) {
console.log(err);
res.send("后台错误");
} else {
res.writeHead(200, {
"Content-type": "text/html",
Connection: "keep-alive",
});
res.end(data);
}
});
});