2

I have a domain. I have two applications using this domain:

www.mydomain.com/first

www.mydomain.com/second

I do a redirect from www.mydomain.com/first to www.mydomain.com/second via the command: res.redirect('/second');

I'd like the application located at www.mydomain.com/second to then do a redirect back to www.mydomain.com/first dynamically (not via hardcoding the address).

So I was thinking that www.mydomain.com/second could get the request referrer field via the command: req.get('Referrer'). I was expecting this to return the value either www.mydomain.com/first, /first or first but it returns undefined.

How do I get a hold of the value?

captainrad
  • 3,760
  • 16
  • 41
  • 74
basickarl
  • 37,187
  • 64
  • 214
  • 335
  • 1
    It sounds to me like you're creating an infinite loop where first redirects to second which redirects to first which redirects to second and so on... – jfriend00 Feb 09 '17 at 09:37
  • 1
    In HTTP it is `referer` (notice one 'r'). – zeronone Feb 09 '17 at 09:38
  • @zeronone - The Express doc says that "the Referrer and Referer fields are interchangeable" with `req.get()` here: https://expressjs.com/en/api.html#req.get – jfriend00 Feb 09 '17 at 09:41
  • @jfriend00 Not really. The second application will set a cookie which is required by the first domain. So it may appear so, but the logic behind it is sound :) – basickarl Feb 09 '17 at 09:45
  • @zeronone Tried both. Same result I'm afraid! – basickarl Feb 09 '17 at 09:48
  • 1
    I think you're banking on something that isn't in the spec. From the little bit I've read on this, the Referer header when redirecting to /second is not set to be /first. If what you're trying to do is to let your server know that the when the request comes in for /second that it came from a redirect, then you should either set a cookie or use a query parameter on the redirect URL. Your server can then see that. A query parameter would probably be better because it's temporal, but you could also set a cookie for the server to see and then remove the cookie when it is seen. – jfriend00 Feb 09 '17 at 10:06

1 Answers1

0

Because you want both applications to live on the same domain you can create a wrapper app that holds both 'children'.

var express = require('express');

var app1 = express();
app1.get('/first', function(req, res) {
  res.send('response from the first app');
})

var app2 = express();
app2.get('/second', function(req, res) {
  res.send('response from the second app');
});

var app = express();
app.use(app1);
app.use(app2);

app.listen(1337);

Because both apps now live on the same domain, you can redirect to what ever url you want as if it was within the same app.

R. Gulbrandsen
  • 3,648
  • 1
  • 22
  • 35