Reputation: 1419
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
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
Reputation: 11047
Try this
app.get('/', function(req, res) {
res.send(req.connection.remoteAddress);
});
Upvotes: 0