1

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.

user87267867
  • 1,409
  • 3
  • 18
  • 25
  • What have you tried so far? Take a look at these questions [How can I get the user's IP address using Node.js](http://stackoverflow.com/questions/8107856/how-can-i-get-the-users-ip-address-using-node-js), [Node.js: Get client's IP](http://stackoverflow.com/questions/19266329/node-js-get-clients-ip) – The Scrum Meister Nov 28 '13 at 06:01
  • One second, you want the **client IP**, or the **current machine IP**? – The Scrum Meister Nov 28 '13 at 06:06
  • ping the domain you will see its IP – meda Nov 28 '13 at 06:09
  • @The Scrum Meister I need the client IP. I used request.connection.remoteAddress and request.headers['X-Forwarded-For'], but Im not getting the exact IP. – user87267867 Nov 28 '13 at 07:50
  • Is it possible to get the IP from the request url using connect? – user87267867 Nov 28 '13 at 08:07

2 Answers2

3

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.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Nitzan Shaked
  • 13,460
  • 5
  • 45
  • 54
0

Try this

app.get('/', function(req, res) {
      res.send(req.connection.remoteAddress);
    });
Damodaran
  • 10,882
  • 10
  • 60
  • 81