0

エラー ハンドラ:

//error handler
app.use(function(err, req, res, next){
    console.error({error: err});
    //res.status(500);
    res.format({
        'text/html': function() {
            res.render('error', {error: err.message});
        },
        'application/json': function() {
            res.json({'error': err.message});
        }
    });
});

ルート 404:

exports.someRoute = function(req, res, next) {
    ...
    if (!page) {
        return next(new Error('Page not found.')));
    }
    ...
};

エラー ハンドラで、適切なエラー ステータス コードを取得するにはどうすればよいですか?

4

1 に答える 1

0

You need to put that information into the error object somehow, either with a simple property like error.status or base it on the class hierarchy like error instanceof NotFound.

However, a 404 is so common that I'd just add logic to req to handle it with something like req.notFound(message) or even just use next('route') with your last route handling the 404.

Anyway, lots of options.

于 2013-10-23T03:46:19.403 に答える