根据这篇文章,我为我的API中间件实现了一个异步中间件句柄:https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016
以下是我的中间件示例:
router.post("/registerVendor", asyncMiddleware(async (req, res, next) => {
const response = await fetchCallWrapper("registerVendor", req.body, 1);
return res
.status(response.status)
.json(response.data);
}));
到目前为止,一切都很好。现在,我想在主server.js表达文件的“常规”路由处理程序内调用提取。如果我可以使用相同的异步中间件处理程序,我现在会感到困惑,或者这是一个问题,因为我缺少“ next”参数?
例如:
server.get("/admin", csrfProtection, redirectIfNoAdmin, asyncMiddleware(async (req,res) => {
res.locals.adminData = await fetchCallWrapper("getProfilesForAdmin", {user: req.session.user.id}, 1);
return handle(req, res);
}));