gpt4 book ai didi

node.js - 发送自定义值以连接兼容的中间件?

转载 作者:太空宇宙 更新时间:2023-11-03 23:44:41 25 4
gpt4 key购买 nike

我有一个 Express 服务器,我正在尝试编写一些中间件来检查用户是否具有指定级别,但我不知道如何将 level 值传递给中间件。

示例 app.js:

app.get('/restricted',verify.auth('4') , routes.index);

示例 auth.js

exports.verify = function(req, res, next, requiredLevel) {
if(userLevel>=requiredLevel){next};
}

最佳答案

试试这个

verify.auth=function(requiredLevel){
return function(req,res,next){
// requiredLevel is visible here ..
// manipulate req,res
//..
// call next middleware
next();
}
}

还可以使用 session ,这样您就不必让用户指定用户级别。

关于node.js - 发送自定义值以连接兼容的中间件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15599495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com