How to specify HTTP error code using Express.js?

I have tried:

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

and:

app.get("https://stackoverflow.com/", function(req, res, next) {
    res.statusCode = 400;
    var e = new Error('error message');
    next(e);
});

but always an error code of 500 is announced.

12 Answers
12

Leave a Comment