📜  如何在路由节点之间共享变量 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:30.438000             🧑  作者: Mango

代码示例1
var router = require("express").Router()

var varSetMiddleware = function(res,req,next){
  router.variable = 100
  // set the variable inside the router object 
  // now you can access it anywhere in this specific router
  next()
}

router.get("/first_view", function (req, res) {
  // something  
  console.log(router.variable) //100
  res.send("something")
})

router.get('/second_view', function (req, res) {
  // something
  console.log(router.variable) //100
  res.send("something")

module.exports = router