[英]Express pass parameter to async middleware
I have a synchronous middleware to authorize role. 我有一个同步中间件来授权角色。 It's set up like this:
设置如下:
Route: 路线:
router.get("/test", [authorizeRole("tier1", "tier2")], function(req, res) {})
Middleware: 中间件:
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
};
};
But nothing works. 但是什么都行不通。
How can i make the middleware 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 要使用
await
您需要使用async
关键字声明函数,因此应该可以使用
module.exports = function authorizeRoles(...role) {
return async (request, response, next) => {
// now you can use `await` here
};
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.