我正在尝试了解如何加载和呈现基本 HTML 文件,因此我不必编写如下代码:
response.write('...<p>blahblahblah</p>...');
最佳答案
我刚刚发现一种方法是使用fs library .我不确定它是否是最干净的。
var http = require('http'),
fs = require('fs');
fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8000);
});
基本概念只是原始文件读取和转储内容。不过,仍然有更清洁的选择!
关于html - 在 Node.js 中加载基本 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4720343/