Redirect inside Router is not relative

I have this router:

var campaign = express.Router();

campaign.post('/', function(req, res) {
  // enter new subscription here...
  res.redirect('welcome');
});

campaign.get('/welcome', function(req, res) {
  res.send('welcome!');
});

app.use('/my-newest-campaign', campaign);

The res.redirect over there does not redirect to /my-newest-campaign/welcome, but to /welcome instead. Is this a bug or is there something else that I can do short of res.redirect('/my-newest-campaign/welcome') to do a router-relative redirect?

Author: Fantashit

1 thought on “Redirect inside Router is not relative

  1. See #2086, the redirect is given to the browser as-is, and as such is relative to the actual URL the user is on at the time of the redirect. If you user is at the URL /foo/bar and you send back res.redirect('baz') the browser will send them to /foo/baz; conversely if the user is at the URL /foo/bar/ and you do the same redirect, the browser will send them to /foo/bar/baz.

    The behavior is identical to if you had <a href="baz"> on that page.

Comments are closed.