0

I need to perform a redirect from a route to another route and pass some data to the latter route. I'm using query strings to pass data between routes, but I'd like to avoid this.

My current solution

I'm using res.redirect and url.format() to pass the data between routes as a query string.

Current code:

app.get('x-foo', (req, res) => {
  const foo = '1234';

  res.redirect(url.format({
     pathname: 'x-bar',
     query: {
       foo: foo
     }
   }));
})

app.get('x-bar', (req, res) => {
  console.log(req.query.foo); // logs '1234'
  res.render(...some page)
});

Why I'd like to avoid it

I'd like to avoid this method because I don't want the query string to appear in the user's browser.

Is there any other way to do this that doesn't involve query strings?

nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
  • is the 2nd route in the same domain as the 1st? if o you can set httpOnly secure cookies to be picked by the 2nd route and then removed. if not this a similar thread although different technology it is the same situation https://stackoverflow.com/questions/5576619/php-redirect-with-post-data – Dayan Moreno Leon Jun 24 '17 at 16:07
  • That's still a bit convoluted. Isn't there a way to do this by Express routing? – nicholaswmin Jun 24 '17 at 16:08
  • Are you using sessions? Because this is [basically] as typical a scenario as it gets. – Brad Christie Jun 24 '17 at 16:13
  • @BradChristie No. – nicholaswmin Jun 24 '17 at 16:18
  • @BradChristie I'm just thinking I might be able to get away with this by using the `body` instead. – nicholaswmin Jun 24 '17 at 16:20
  • @NicholasKyriakides Not for a GET request... Either way, it's basically either a) Use queryString, b) use session, c) use cookie, d) Use something more elaborate like a local file or something else that wouldn't be thread/request safe – Brad Christie Jun 24 '17 at 16:23

0 Answers0