Express 错误中间件发送响应
Express error middleware sending response
在我的 express
应用程序中,当到达错误中间件时它仍然执行 res.json
之后的第二个中间件函数。 docs 表示:
The methods on the response object (res) in the following table can send a response to the client, and terminate the request-response cycle. If none of these methods are called from a route handler, the client request will be left hanging.
其中 res.json
是 table 中将终止请求-响应周期的方法之一。我也没有在我的快速错误中间件中调用 next
router.use((err, req, res, next) => {
res.json({
success: false,
message: 'fail: email already exists'
});
});
//test still gets logged even though the express error middleware is executed
router.post('/', (req, res , next) => {
console.log('test');
});
问题:
为什么我在错误中间件中没有调用next并执行res.json()
,第二个express
中间件仍然执行?
根据 documentation on error handling 您必须指定错误处理中间件 last 才能正常工作。
很可能是因为它带有错误签名而变得混乱。
在我的 express
应用程序中,当到达错误中间件时它仍然执行 res.json
之后的第二个中间件函数。 docs 表示:
The methods on the response object (res) in the following table can send a response to the client, and terminate the request-response cycle. If none of these methods are called from a route handler, the client request will be left hanging.
其中 res.json
是 table 中将终止请求-响应周期的方法之一。我也没有在我的快速错误中间件中调用 next
router.use((err, req, res, next) => {
res.json({
success: false,
message: 'fail: email already exists'
});
});
//test still gets logged even though the express error middleware is executed
router.post('/', (req, res , next) => {
console.log('test');
});
问题:
为什么我在错误中间件中没有调用next并执行res.json()
,第二个express
中间件仍然执行?
根据 documentation on error handling 您必须指定错误处理中间件 last 才能正常工作。
很可能是因为它带有错误签名而变得混乱。