Node.js app.js不是显示HTML页面,而是HTML代码

我的问题与我的app.js节点不显示我的HTML页面,而是我的HTML代码。 我真的不知道该怎么做,所以一些帮助将不胜感激。 我想能够从我的公用文件夹加载HTML,JavaScript和CSS。 这是我的app.js代码:

var http = require("http"), url = require("url"), path = require("path"), fs = require("fs") http.createServer(function(request, response) { var uri = url.parse(request.url).pathname , filename = path.join(process.cwd(), uri); path.exists(filename, function(exists) { if(!exists) { response.writeHead(404, {"Content-Type": "text/plain"}); response.write("404 Not Found\n"); response.end(); return; } if (fs.statSync(filename).isDirectory()) filename += 'public/index.html'; fs.readFile(filename, "binary", function(err, file) { if(err) { response.writeHead(500, {"Content-Type": "text/plain"}); response.write(err + "\n"); response.end(); return; } response.writeHead(200); response.write(file, "binary"); response.end(); }); }); }).listen(process.env.PORT, process.env.IP); console.log("Static file server running./\CTRL + C to shutdown"); 

您没有指定内容types,如果您尝试:

 fs.readFile(filename, "binary", function(err, file) { if(err) { response.writeHead(500, {"Content-Type": "text/plain"}); response.write(err + "\n"); response.end(); return; } response.writeHead(200, {'Content-Type': 'text/html'}); response.write(file, "binary"); response.end(); }); }); }).listen(process.env.PORT, process.env.IP); 

这应该工作。 如果你想在html中显示错误页面,你也需要更新这个内容types。