Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,558 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!