从中间件nodejs传递参数

时间:2017-01-16 10:55:03

标签: javascript mysql node.js express middleware

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")
});

1 个答案:

答案 0 :(得分:0)

您可以将数据添加到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)
    });