场景:登录接口,当不添加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;
}
})