浮云间
要指示缺少文件/资源并提供404页面,您无需重定向。在同一请求中,您必须生成响应,状态代码设置为404,404 HTML页面的内容作为响应正文。以下是在Node.js中演示此示例的示例代码。var http = require('http'),
fs = require('fs'),
util = require('util'),
url = require('url');var server = http.createServer(function(req, res) {
if(url.parse(req.url).pathname == '/') {
res.writeHead(200, {'content-type': 'text/html'});
var rs = fs.createReadStream('index.html');
util.pump(rs, res);
} else {
res.writeHead(404, {'content-type': 'text/html'});
var rs = fs.createReadStream('404.html');
util.pump(rs, res);
}});server.listen(8080);