简体   繁体   English

从中间件nodejs传递参数

[英]passing parameter from middleware nodejs

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

I need to pass user_name as a parameter from next(), the function which it gets called is as bellow, 我需要将user_name作为next()的参数传递,它被调用的功能如下所示,

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

You can add data to the req object 您可以将数据添加到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)
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM