我已将Node Express应用程序精简到最低限度。我什至不再定义路线。当我转到任何路由时,例如localhost:3000 / sdfsdf,我都会得到预期的输出:
{“消息”:“未找到”,“错误”:{“状态”:404}}
但是,响应的HTTP状态代码为200。我希望它是404。我缺少什么?
var express = require('express');
var path = require('path');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// development error handler
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
//res.set('Content-Type', 'application/json');
res.status(err.status || 500);
res.status('error').json({
message: err.message,
error: err
});
});
}
// production error handler
app.use(function(err, req, res, next) {
res.set('Content-Type', 'application/json');
res.status(err.status || 500);
res.status('error').json({
message: err.message,
error: {'env': 'prod'}
});
});
module.exports = app;
res.status(err.status || 500);
res.status('error').json({
您最终res.statuscode
用' error
'覆盖。改成
res.status(err.status || 500);
res.json({
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句