我已经使用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");