Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: Get client's IP

req.connection.remoteAddress, req.headers['x-forwarded-for'], req.ip, req.ips, what does it all mean?

Is there a straight forward way to simply get the IP address of the client/user-agent making the request to my site in Node.js/Express? I'm not understanding all the proxy stuff or what all the differences between all the properties of the req object. Also, I don't understand what the 'trust proxy' option is for Express.

Could someone give me a straight forward explanation to what the difference is between all of these properties, and answer how I can just simply get the client's IP?

like image 675
Sam Avatar asked Oct 09 '13 08:10

Sam


People also ask

How do I find my IP address on Express?

Express provides user's IP information in the request object. If you are using a reverse proxy in the front of Express web server ( You should in the production server ) you can retrieve the IP address in the x-forwared-for header, assuming you have configured your reverse proxy server correctly.

How do I find my local IP node?

Any IP address of your machine you can find by using the os module - and that's native to Node. js: var os = require('os'); var networkInterfaces = os. networkInterfaces(); console.


1 Answers

req.ip is the straightforward way to get the client's IP address in Express. You can see the logic it uses (which involves grabbing the first item from the array of proxy addresses req.ips, where that array is constructed from the x-forwarded-for headers) here.

like image 177
Dan Kohn Avatar answered Sep 21 '22 23:09

Dan Kohn