user87267867
user87267867

Reputation: 1419

Get the IP address from request url

How to get the IP address from HTTP request url or the client IP from where my node code is running.

http://127.0.0.1:1000/samp/

The client IP can be an nginx IP or public IP. Any help on this will be really helpful.
Thanks.

Upvotes: 1

Views: 3861

Answers (2)

Nitzan Shaked
Nitzan Shaked

Reputation: 13598

Assuming you want the client ip, you'll need to first decide if you trust "X-Forwarded-For" headers (that is: you are behind a reverse proxy that you yourself set up and which you trust, or you trust the proxies along the way).

If so, then get the ip in req.headers['x-forwarded-for'] (this could potentially be a list).

If not, then req.connection.remoteAddress is the answer.

Upvotes: 3

Damodaran
Damodaran

Reputation: 11047

Try this

app.get('/', function(req, res) {
      res.send(req.connection.remoteAddress);
    });

Upvotes: 0

Related Questions