gpt4 book ai didi

javascript - 从中间件nodejs传递参数

转载 作者:行者123 更新时间:2023-11-29 19:33:05 24 4
gpt4 key购买 nike

if (token_count == 1) {
var user_name = rows[0].user_name;
next();
} else {
data = {
message :"Invalid Token"
}
res.send(data);
}

我需要将 user_name 作为参数从 next() 传递,它被调用的函数如下,

router.post('/dashboard', function (req, res) {
// user_name must be fetched here
console.log("middleware next")
});

最佳答案

您可以将数据添加到 req 对象

if (token_count == 1) {
var user_name = rows[0].user_name;
req.user_name = user_name;
next();
}else{
data = {
message :"Invalid Token"
}
res.send(data);
}


router.post('/dashboard', function (req, res) {
// user_name must be fetched here
console.log(req.user_name)
});

关于javascript - 从中间件nodejs传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41674735/

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