const http = require("http");
const url = require("url");
const fs = require("fs");
http
.createServer(function (req, res) {
const pathname = url.parse(req.url, true).pathname;
if (pathname == "/favicon.ico") return res.end();
let filename;
if (pathname == "/") filename = "./index.html";
else filename = "." + pathname + ".html";
console.log(filename);
fs.readFile(filename, function (err, data) {
if (err) {
fs.readFile("./404.html", function (err, data) {
res.writeHead(200, { "Content-Type": "text/html" });
res.write(data);
return res.end();
});
}
res.writeHead(200, { "Content-Type": "text/html" });
res.write(data);
return res.end();
});
})
.listen(8080, () => {
console.log("Server made");
});
I'm trying to read the 404 html file whenever I'm accessing a non-specified file in the if(err) condition. The error that I'm getting is TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received undefined
. Why do I have this error?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…