方法一:
使用 node.js 运行 index.html 文件,将以下代码复制粘贴到应用文件夹中的 server.js 文件(层次结构之上)
var http = require('http');
var fs = require("fs");
http.createServer(function(request, response) {
if(/(.*?).css$/.test(request.url.toString())){
sendFileContent(response, request.url.toString().substring(1), "text/css");
}else if(/(.*?).js$/.test(request.url.toString())){
sendFileContent(response, request.url.toString().substring(1), "text/javascript");
}else if(/(.*?).html$/.test(request.url.toString())){
sendFileContent(response, request.url.toString().substring(1), "text/html");
}else if(request.url.toString().substring(1) == ''){
sendFileContent(response, "index.html", "text/html");
}
}).listen(3000);
function sendFileContent(response, fileName, contentType){
fs.readFile(fileName, function(err, data){
if(err){
response.writeHead(404);
response.write("Not Found!");
}
else{
response.writeHead(200, {'Content-Type': contentType});
response.write(data);
}
response.end();
});
}
然后从 app 文件夹运行 node server.js。
您的 html 文件将在 localhost:3000 中提供
方法二:
使用 http-server。按照此link 中的步骤全局安装 http-server 并从您的应用程序文件夹运行 cmd
http-server -a localhost -p 8000 -c-1 ./app
您的 index.html 文件将在 localhost:8000 中提供服务
注意:上述方法中.listen和-p中的端口号可以更改。