How can I retrieve the public IP of a user using Node.js?

3 points by bqc ↗ HN
I’m developing a Node.js application and need to obtain the public IP address of the users accessing the app. What are the most reliable methods or libraries to achieve this? Are there any security considerations I should be aware of when implementing this feature?

12 comments

[ 4.2 ms ] story [ 35.9 ms ] thread
That's more of a function of your web server's access logs (or your hosted service's analytics, if in the cloud) than Node.js itself.
are the users using http/https to access the app?
yes
are u a robot? or simply answer whatever comes to mind without reading the question?
Would it be possible to use fetch to call out to the whatismyip.com API? Or scrape it?

https://www.whatismyip.com/api/

If they're running Node, that probably means it's not in the browser.

If it's a server on the internet that browsers connect to (like a standard web app), it already knows all the connecting IPs from the HTTP requests.

If it's some sort of wrapper running Node locally on client computers, well, presumably it would communicate with your server anyway (or else what good are the analytics)? In that case your server would still have their IP address.

You don't need to use a third party like whatismyip.com unless for some reason you didn't want your server involved at all, you just want to find their public IP and then store it locally (for some reason) on that same computer. Hard to imagine why you'd do that unless you're building some sort of malware or long-term user tracking.

I want Public IP not the local network IP.

const userIP = req.ip || req.connection.remoteAddress || req.headers['x-forwarded-for'];

Their public IP is how they reach your server, unless you're on the same computer or local network as them. Are you? It's not clear from your post where Node is running relative to your user.
The workflow goes like this: I’m working on a React application where users make API requests and send data. I need to capture the user’s public IP address before storing the data. Currently, my method returns the local IP address, but I need the public IP address instead.
I'm still not sure if this is what you're asking, but have you already tried checking `req.headers['x-forwarded-for']`? Normally node just knows the incoming request IP (that's how it was reached to begin with), but if you're behind another server or proxy, sometimes the original IP gets forwarded instead.

If that's not what you're asking... can you please clarify your client-server architecture (if there even is one?)

Where is Node running? Is it on an internet-facing server, acting as a backend? Or are you doing some sort of local desktop app? Is Node using its own http systems to act as a server, or is there some other web server or reverse proxy running in front of it?

Is React running on the client's browser, separate from Node altogether? Or are you using React on the backend too (like in Next or similar)?

req.ip, but also look at the x-forwarded-for header if you are using a proxy server. This header can have multiple ip's when you have say nginx terminate ssl and use cloudflare.
Unless they use a proxy every internet interaction contains the originators ip address. It's how you are able to respond to it.

Assuming you are developing node.js on a server.