Javascript Node.js服务器";“404未找到”;发送至404.html页面的消息
我正在使用node.js,我想知道如何显示404.html而不是“404未找到”消息 这是我的server.js:Javascript Node.js服务器";“404未找到”;发送至404.html页面的消息,javascript,html,node.js,Javascript,Html,Node.js,我正在使用node.js,我想知道如何显示404.html而不是“404未找到”消息 这是我的server.js: var http = require("http"), url = require("url"), path = require("path"), fs = require("fs") port = process.argv[2] || 8888; http.createServer(function(request, response) { var uri = url.par
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
port = process.argv[2] || 8888;
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/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(parseInt(port, 10));
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");
正如您所看到的,它只是一个静态文件服务器,我没有使用express.js或任何东西。只需使用fs.readFile加载404.html,并使用response.write为其提供服务
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
port = process.argv[2] || 8888;
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) {
fs.readFile('404.html', "binary", function(err, file) {
if(err) {
response.writeHead(404, {"Content-Type": "text/html"});
response.write("404 Not Found\n");
} else {
response.writeHead(404);
response.write(file, "binary");
}
response.end();
return;
}
}
if (fs.statSync(filename).isDirectory()) filename += 'public/Index/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(parseInt(port, 10));
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");`enter code here`
只为200美元而做。。。
读取文件404.html并将其写入响应,只需将代码404设置为writeHead
response.writeHead(404, {
'Location': 'your/404/path.html'
//add other headers here...
});
response.end();
还是用一行
response.redirect('your/404/path.html');
H i
在您的404案例中
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
你可以换成
response.writeHead(404, {"Content-Type": "text/html"});
response.write(HTMLDATA);
response.end();
“HTMLDATA”是HTML字符串或对已收集文件的引用
response.writeHead()
总是在response.write()之前设置
另请参见我们已将响应类型设置为“text/html”
这可能是您需要的更多内容
fs.readFile('404.html', function(error, data) {
res.writeHead(404, {'content-type': 'text/html'});
res.end(data);
});
这是对的!非重定向,状态为404,内容类型为text/html