(Express)如何在 express.Router().get 中将一个值从函数传递给另一个函数

(Express)How to pass a value from function to another one in express.Router().get

如何在 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
}, )

您可以使用 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).

所以在你的情况下

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