Eric Bergman-Terrell's Blog

Node.js / Express Programming Tip: How to Determine IP Address from an HTTP Request
August 20, 2015

You may find that req.connection.remoteAddress does not have the user's IP address, especially in a hosted environment. The solution is to use req.headers['x-forwarded-for'], if available. Otherwise use req.connection.remoteAddress. For example:

exports.getIPAddress = function(req) {
    return req.headers['x-forwarded-for'] || req.connection.remoteAddress;
};

there's no place like 127.0.0.1
There's no place like 127.0.0.1!

Keywords: Node.js, Express, IP Address, Javascript

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
EBTCalc (Android) Version 1.53 is now availableMay 19, 2024
Vault 3 Security EnhancementsOctober 24, 2023
Vault 3 is now available for Apple OSX M2 Mac Computers!September 18, 2023
Vault (for Desktop) Version 0.77 ReleasedMarch 26, 2023
EBTCalc (Android) Version 1.44 is now availableOctober 12, 2021
Vault (Desktop) Version 0.72 ReleasedOctober 6, 2021
EBT Compass is Now Available for Android DevicesJune 2, 2021