How to specify HTTP error code?

后端 未结 11 2655
余生分开走
余生分开走 2020-12-02 13:48

I have tried:

app.get(\'/\', function(req, res, next) {
    var e = new Error(\'error message\');
    e.status = 400;
    next(e);
});

and:

11条回答
  •  生来不讨喜
    2020-12-02 14:22

    Old question, but still coming up on Google. In the current version of Express (3.4.0), you can alter res.statusCode before calling next(err):

    res.statusCode = 404;
    next(new Error('File not found'));
    

提交回复
热议问题