在express中使用async/await来控制异步流程,为了防止异常写了很多try/catch,不知道express能否在入口文件中处理所有程序执行过程中抛出的异常。
回复 (2)
S
已自行解决
Z
本楼正解
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){
// 哈哈
}));
参与回复
登录后即可参与回复。登录