Passing variables to the next middleware using next() in Express.js

后端 未结 6 1432
长情又很酷
长情又很酷 2020-11-27 10:11

Well, my question is I want to pass some variable from the first middleware to another middleware, and I tried doing this, but there was \"req.somevariable is a

6条回答
  •  温柔的废话
    2020-11-27 10:31

    Attach your variable to the req object, not res.

    Instead of

    res.somevariable = variable1;
    

    Have:

    req.somevariable = variable1;
    

    As others have pointed out, res.locals is the recommended way of passing data through middleware.

提交回复
热议问题