7

What is the equivalent to window.location.protocol and window.location.host in NodeJS/ExpressJS?

I'm trying to redirect the url back to my site using a third party API.

ayeseeem
  • 149
  • 1
  • 6
Brown PO
  • 457
  • 3
  • 9
  • 13
  • 1
    Possible duplicate of [Nodejs - Redirect url](http://stackoverflow.com/questions/4062260/nodejs-redirect-url) – Kld Jul 19 '16 at 13:28

1 Answers1

3

The NodeJS API url.format(urlObject) method returns a formatted URL string derived from urlObject.

var url = require('url');

function getFormattedUrl(req) {
    return url.format({
        protocol: req.protocol,
        host: req.get('host')
    });
}

res.redirect(getFormattedUrl(req));
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
  • Isn't getting the **host** name from the **Host** headers secure? @YosvelQuinteroArguelles [Check this one](https://www.meziantou.net/how-to-implement-password-reset-feature-in-a-web-application.htm#-be-careful-when-cre) – Dev_noob Aug 08 '20 at 20:28