場景:登錄接口,當不添加async
的時候可以正常訪問,添加之後返回Not Found
router.post('login', async (ctx, next) => {
console.log('login')
await userService.findUserByName().then((res) => {
console.log('findUserByName', res)
ctx.body = res;
})
});
這是因爲添加了中間件沒有添加async
。使用router.use
中間件的函數不管是不是異步的都需要使用async
router.use(async (ctx, next) => {
if(await check.chenckLogin(ctx)) {
await next();
}else {
ctx.body = check.notLoginResponse;
}
})