【发布时间】:2022-01-27 00:09:39
【问题描述】:
这是有问题的代码:
import express from 'express';
// Non existing routes
app.use((req: Request, res: Response, next: NextFunction) => {
return next(new Error('Test error));
});
// Error handling
// Arbitrary example
app.use((error: any, req: Request, res: Response) => {
res
.status(500)
.json({ 'Server error' });
});
问题是,例如,当在缺少路由中间件的 next() 函数中传递错误时,它不会到达错误处理中间件,结果是 html 而不是 json 返回给客户端。
【问题讨论】:
标签: typescript express error-handling middleware