Shamoon Shamoon - 7 months ago 13
Node.js Question

How to determine a user's IP address in node

How can I determine the IP address of a given request from within a controller? For example (in express):'/get/ip/address', function (req, res) {
// need access to IP address here


In your request object there is a property called connection, which is a net.Socket object. The net.Socket object has a property remoteAddress, therefore you should be able to get the IP with this call:


See documentation for http and net


As @juand points out in the comments, the correct method to get the remote IP, if the server is behind a proxy, is request.headers['x-forwarded-for']