【发布时间】:2018-05-04 16:54:23
【问题描述】:
我使用fs.readFileSync() 来读取 HTML 文件并且它正在工作。但是我在使用fs.readFile() 时遇到了问题。你能帮我解决这个问题吗?任何帮助将不胜感激!
- 使用
fs.readFileSync():
const http = require("http");
const fs = require("fs");
http.createServer((req, res) => {
res.writeHead(200, {
"Content-type": "text/html"
});
const html = fs.readFileSync(__dirname + "/bai55.html", "utf8");
const user = "Node JS";
html = html.replace("{ user }", user);
res.end(html);
}).listen(1337, "127.0.0.1");
- 使用
fs.readFile()。为什么不能读取 HTML 文件?
const http = require("http");
const fs = require("fs");
http.createServer((req, res) => {
res.writeHead(200, {
"Content-type": "text/html"
});
const html = fs.readFile(__dirname + "/bai55.html", "utf8");
const user = "Node JS";
html = html.replace("{ user }", user);
res.end(html);
}).listen(1337, "127.0.0.1");
【问题讨论】: