【发布时间】:2012-01-13 15:55:10
【问题描述】:
我想在我的 Express/Node 服务器上模拟 404 错误。我该怎么做?
【问题讨论】:
-
“模拟”与“真实”有何不同?
标签: javascript node.js express http-status-codes
我想在我的 Express/Node 服务器上模拟 404 错误。我该怎么做?
【问题讨论】:
标签: javascript node.js express http-status-codes
从 Express 4.0 开始,有一个专用的sendStatus function:
res.sendStatus(404);
如果您使用的是早期版本的 Express,请改用 status function。
res.status(404).send('Not found');
【讨论】:
res.status(404).render('error404')
res.status(404); 不会发送响应 AFAIK。它需要与某些东西链接在一起,例如res.status(404).end(); 或您的第二个示例,或者需要在后面加上例如res.end();, res.send('Not found');
res.sendStatus(404)
而不是像旧版本的Express那样使用res.send(404),新的方法是:
res.sendStatus(404);
Express 将发送带有“未找到”文本的非常基本的 404 响应:
HTTP/1.1 404 Not Found
X-Powered-By: Express
Vary: Origin
Content-Type: text/plain; charset=utf-8
Content-Length: 9
ETag: W/"9-nR6tc+Z4+i9RpwqTOwvwFw"
Date: Fri, 23 Oct 2015 20:08:19 GMT
Connection: keep-alive
Not Found
【讨论】:
res.status(404) 而不是res.sendStatus(404)。
res.sendStatus(404) 是正确的。相当于res.status(404).send()
res.sendStatus(404); 等同于 res.status(404).send('Not Found')
您不必模拟它。我相信res.send 的第二个参数是状态码。只需将 404 传递给该参数即可。
让我澄清一下:根据the documentation on expressjs.org,似乎传递给res.send() 的任何数字都将被解释为状态码。所以从技术上讲,你可以侥幸逃脱:
res.send(404);
编辑:我的错,我的意思是 res 而不是 req。应该在响应中调用它
编辑: 从 Express 4 开始,send(status) 方法已被弃用。如果您使用 Express 4 或更高版本,请改用:res.sendStatus(404)。 (感谢@badcc 在 cmets 中的提示)
【讨论】:
res.send(404, "Could not find ID "+id)
.status(404).send('Not found')
根据我将在下面发布的网站,这就是您设置服务器的方式。他们展示的一个例子是:
var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(handle, pathname, response);
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
及其路由功能:
function route(handle, pathname, response) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname](response);
} else {
console.log("No request handler found for " + pathname);
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not found");
response.end();
}
}
exports.route = route;
这是一种方式。 http://www.nodebeginner.org/
他们从另一个站点创建一个页面,然后加载它。这可能是您正在寻找的更多内容。
fs.readFile('www/404.html', function(error2, data) {
response.writeHead(404, {'content-type': 'text/html'});
response.end(data);
});
【讨论】:
在Express site 中,定义一个 NotFound 异常,并在您想要一个 404 页面或在以下情况下重定向到 /404 时抛出它:
function NotFound(msg){
this.name = 'NotFound';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
NotFound.prototype.__proto__ = Error.prototype;
app.get('/404', function(req, res){
throw new NotFound;
});
app.get('/500', function(req, res){
throw new Error('keyboard cat!');
});
【讨论】:
app.use(function(err, res, res, next) { if (err.message.indexOf('NotFound') !== -1) { res.status(400).send('Not found dude'); }; /* else .. etc */ });
IMO 最好的方法是使用 next() 函数:
router.get('/', function(req, res, next) {
var err = new Error('Not found');
err.status = 404;
return next(err);
}
然后错误由您的错误处理程序处理,您可以使用 HTML 很好地设置错误样式。
【讨论】: