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 Users: A Desktop Version is Now Available!October 25, 2019
Python Script to Audit MediaMonkey TranscodingAugust 15, 2019
How to decompile Java code with JetBrains IntelliJ IDEA (2018.2.3, Windows 10)October 5, 2018
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