user1400312
user1400312

Reputation:

req.headers.host in Express.js returns 127.0.0.1

I am trying to get the host accessing my site in Express.js, I am using the following code

app.get('/', function(req,res){
    console.log(req.headers.host)
});

Although this code returns 127.0.0.1:1000 which is the correct port, but it's not the external address accessing it, any reason why this happens?

Thanks

Upvotes: 0

Views: 3757

Answers (2)

Konstantin Pelepelin
Konstantin Pelepelin

Reputation: 1563

Either you've pointed your browser directly to http://127.0.0.1:1000 or you're using a local reverse proxy. In the second case your proxy must be configured to set a proper Host header.

Upvotes: 0

Hector Correa
Hector Correa

Reputation: 26680

Not every client has a host name, as indicated in this other question you can try to obtain the client IP address with the following code, though:

req.headers['x-forwarded-for'] || req.connection.remoteAddress

Upvotes: 1

Related Questions