0

i have one url that is rendering data and url contains some parameter. so when in that same route i can use those parameter but i cant use it in another routes. so can someone help me with how to transfer data from one routes to another.

router.get('/token/:tokenvalue', function(req, res, next){
  var token = req.params.tokenvalue;
  // globalVariable.token = token;
  // console.log(globalVariable.token);
  req.token = token;
  res.render('candidate.ejs');
})

after showing this page i am using google login so i cant store this token to req variable or somewhere else. so can someone suggest me how to resolve this issue.

Ankit Baid
  • 71
  • 3
  • 8
  • 1
    Store the token in the users session so you don't have to pass it around routes like that – Sterling Archer Oct 21 '17 at 16:26
  • thanks a lot @SterlingArcher. its working now. – Ankit Baid Oct 21 '17 at 16:28
  • You can use [`response.locals`](https://expressjs.com/en/api.html#res.locals) to share info between middleware of the same request – MinusFour Oct 21 '17 at 16:29
  • 1
    Possible duplicate of [passing variables to the next middleware using next() in expressjs](https://stackoverflow.com/questions/18875292/passing-variables-to-the-next-middleware-using-next-in-expressjs) – Mark Oct 21 '17 at 16:43

1 Answers1

0

There is a NPM package build for that called 'connect-flash'

$ npm install connect-flash

Than in your app.js || server.js ( your main file which boots your server )

var express = require('express');
var flash = require('connect-flash');
var app = express();
app.use(flash());

app.get('/login', function(req, res){
  // Set a flash message by passing the key, followed by the value, to req.flash().
  req.flash('username', 'Gaurav Gupta')
  res.redirect('/profile');
});

app.get('/profile', function(req, res){
  // Get an array of flash messages by passing the key to req.flash()
  let message = req.flash('username')
  res.render('index', { message: message }); // or {message} only es6 feature
});

The flash is a special area of the session used for storing messages. Messages are written to the flash and cleared after being displayed to the user. The flash is typically used in combination with redirects, ensuring that the message is available to the next page that is to be rendered.

GouravGupta
  • 143
  • 2
  • 5