表示next()标题错误
在一个路线我有这个:
if (elements.length <= 0) { var msg = 'no elements found'; console.error(msg); var err = new Error('Not found'); err.status = 404; err.message = msg; next(err); } console.log('Found ' + elements.length + ' elements'); res.setHeader('Content-Type', 'application/json'); /*error*/ res.status(200).json(elements); res.end();
在app.js中最后定义的error handling程序:
// development error handler app.use(function(err, req, res, next) { res.type('application/json'); res.status(err.status || 500); res.json({ message: err.message, error: err }); });
我看到这个错误是在json中作为响应发送的。 但是我在标有/ error /的行中出现这个错误:
发送后无法设置标题。
为什么快递从error handling程序返回? 我可以看到它继续执行路由(从console.log)为什么它继续执行路由?
里面if
陈述正确:
return next(err);