将参数传递给异步中间件 [英] Express pass parameter to async middleware
本文介绍了将参数传递给异步中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个同步中间件来授权角色.设置如下:
I have a synchronous middleware to authorize role. It's set up like this:
路线:
router.get("/test", [authorizeRole("tier1", "tier2")], function(req, res) {})
中间件:
module.exports = function authorizeRoles(...role) {
return (request, response, next) => {
//authorize
};
};
这正在工作.现在,我想更改中间件以使其异步.
This is working. Now i want to change my middleware to make it async.
我不知道如何使中间件异步.我试过了:
I can't figure out how to make the middleware async. I tried:
module.exports = async function authorizeRoles(...role) {
return await (request, response, next) => {
//authorize
};
};
module.exports = async function authorizeRoles(...role) {
return await async (request, response, next) => {
//authorize
};
};
但是什么都行不通.
如何使中间件异步?
推荐答案
我不确定我是否完全理解您的问题.要使用await
,您需要使用async
关键字声明函数,因此应该可以使用
I'm not sure I completely understood your question. To use await
you need to declare your function using the async
keyword, so this should work
module.exports = function authorizeRoles(...role) {
return async (request, response, next) => {
// now you can use `await` here
};
};
这篇关于将参数传递给异步中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文