[英](Express)How to pass a value from function to another one in express.Router().get
How I can pass value from function to another one at router.get如何将值从 function 传递给 router.get 的另一个值
router.get('/someurl', (req, res, next) => {
const token = req.headers.authorization.split(' ')[1] //jwtToken
const jwt = jwt.verify(
token,
jwtSecret
)
...do something to pass value to the next function
}, )
You can use res.locals to do that你可以使用res.locals来做到这一点
An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any).一个 object 包含范围为请求的响应局部变量,因此仅可用于在该请求/响应周期(如果有)期间呈现的视图。
So in your case所以在你的情况下
router.get(
"/someurl",
(req, res, next) => {
const token = req.headers.authorization.split(" ")[1]; //jwtToken
const jwt = jwt.verify(token, jwtSecret);
// pass to res.locals so I can get it in next() middleware
res.locals.token = token;
next();
},
(req, res, next) => {
// inside the next() middleware
// get token from res.locals
const previousToken = res.locals.token;
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.