express如何在入口文件中处理所有的异常
发布于 9 年前 作者 silenceboychen 4121 次浏览 来自 问答

在express中使用async/await来控制异步流程,为了防止异常写了很多try/catch,不知道express能否在入口文件中处理所有程序执行过程中抛出的异常。

2 回复

已自行解决

本楼正解

function ar(cb: (req: express.Request, res: express.Response, next: Function) => any) {
 return function (req: express.Request, res: express.Response, next: Function) {
 cb(req, res, next).catch((error: any) => {
 next(error);
 });
 }
}
router.get('/', ar(async function(req, res){
// 哈哈
}));
回到顶部

AltStyle によって変換されたページ (->オリジナル) /