0

I have tried these three approaches:

req.headers['x-forwarded-for'] || '').split(',')[0]

req.client.remoteAddress

req.ip (with app.enable('trust proxy'))

But I always get ::1 as a result (on localhost). Other solutions also return ::1. I am trying to obtain a real user IP, e.g. 148.62.52.2. How can I get that?

Community
  • 1
  • 1
WebArtisan
  • 3,248
  • 9
  • 31
  • 54
  • 2
    You're getting it already, those are perfect ways to get it, but your address is ::1 because you're using IPv6 and localhost. – cviejo Dec 26 '15 at 18:29
  • i need clients ip, something like 148.62.522 – WebArtisan Dec 26 '15 at 18:34
  • that does not appears to be a valid ip , did you mean 148.62.52.2 – prasun Dec 26 '15 at 18:35
  • i wrote just a random string, but you got my point - i need user ip. – WebArtisan Dec 26 '15 at 18:36
  • 1
    You will get a different IP once you move your app to a production server or your client doesn't reside on the same machine as the server. If client and server are on the same machine you're going to get loopback addresses, typically 127.0.0.1 for IPv4 and ::1 for IPv6. – cviejo Dec 26 '15 at 18:47
  • 1
    Ok, i got it. Thank u, cviejo. – WebArtisan Dec 26 '15 at 18:50
  • Perhaps you don't understand that `localhost` is DNS resolved to either `::1` or `127.0.0.1` depending upon whether you're using IPv4 of IPv6 and that is the actual IP address used for the connection. The external IP address is not used when you're connecting to `localhost` so the connection does not necessarily even know it. In fact, you can connect to `localhost` without even being on a network at all. – jfriend00 Dec 26 '15 at 19:09

1 Answers1

-1

The remote ip will be listed here:

request.connection.remoteAddress

If the server is behind a proxy, you'll need to look here:

request.headers['x-forwarded-for']

If your server is local, and your making a request from the local machine, don't expect to see a remote ip.

Brandon Smith
  • 1,059
  • 7
  • 11