Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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
 
PREVIOUS NEXT
Tagged: #share #variables #routes #node
ADD COMMENT
Topic
Name
9+5 =