如何指定HTTP错误代码? [英] How to specify HTTP error code?
本文介绍了如何指定HTTP错误代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过:
app.get('/', function(req, res, next) {
var e = new Error('error message');
e.status = 400;
next(e);
});
和:
app.get('/', function(req, res, next) {
res.statusCode = 400;
var e = new Error('error message');
next(e);
});
但总是发布500的错误代码。
but always an error code of 500 is announced.
推荐答案
根据Express(版本4+)文档,您可以使用:
Per the Express (Version 4+) docs, you can use:
res.status(400);
res.send('None shall pass');
http://expressjs.com/4x/api.html#res.status
< = 3.8
<=3.8
res.statusCode = 401;
res.send('None shall pass');
这篇关于如何指定HTTP错误代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文