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
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015
Node.js / Express Programming Tip: Detect and Fix Memory LeaksOctober 27, 2015