Answers for "how to share variables between routes node"

0

how to share variables between routes node

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
Posted by: Guest on June-09-2021

Code answers related to "how to share variables between routes node"

Code answers related to "Javascript"

Browse Popular Code Answers by Language