sammy
sammy

Reputation: 343

How do I redirect to an absolute url in express js?

I would like to redirect to an absolute url like so

express.redirect('127.0.0.1:3000/page);

The problem is that this produces the following url:

127.0.0.1:3000/127.0.0.1:3000/page

How can I redirect to an absolute path as described above?

Upvotes: 2

Views: 1603

Answers (1)

sammy
sammy

Reputation: 343

Apparently you need to add "//" in front of the redirect url to make express know it's absolute. Lucky I accidentally figured that out as it doesn't seem to appear in the docs.

A few examples to be clear:

res.redirect('//127.0.0.1:3000');

or

res.redirect('//www.tutaroo.com');

If you try something like

res.redirect('//http://www.tutaroo.com');

you will likely get redirect errors as it will try to look for

https://https//www.tutaroo.com

so be careful with that.

Upvotes: 4

Related Questions