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
.NET Public-Key (Asymmetric) Cryptography DemoJuly 20, 2025
Raspberry Pi 3B+ Photo FrameJune 17, 2025
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