JavaScript

Chris Cooper
Chris Cooper
23,959 Points

Using express, on localhost req.ip is returning ::1 is that right?

I'm developing a web app, using the MEAN stack.

I am trying to create a log of login attempts and in there i want to be able to show the IP address the request has come from.

The express docs say i can use req.ip which takes it from the req.headers['x-forward-for'] value. However, on my localhost that is undefined and req.ip returns ::1.

Is that what should be expected in the localhost environment?

If not, how can i get the Clients IP?

Thanks in Advance, Chris

1 Answer

andren
andren
28,480 Points

::1 is a shortened version of the localhost address of a IPv6 client, the full IP is technically 0:0:0:0:0:0:0:1. It is equivalent to the 127.0.0.1 address for a IPv4 client.

Getting into an explanation of IPv6 and the differences between it and IPv4 is beyond this post. But in short it is a different IP system that has far longer addresses, it was developed mostly to address the fact that there is a pretty limited amount of IPv4 addresses, which means we will eventually run out of addresses to use.

To answer your question more directly, if IPv6 is enabled on your localhost (which it probably is) then the ::1 address is perfectly fine, and the req.ip is likely working exactly as intended.

Chris Cooper
Chris Cooper
23,959 Points

That makes total sense. Thanks a lot for the quick response!