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
Get JetBrains IDEs to Remember Subversion (SVN) CredentialsNovember 3, 2026
Converting a Thinkpad from Windows 11 to Linux MintOctober 29, 2026
Raspberry Pi Camera Trap and Animal Detection ProgramAugust 29, 2026
Python Tip: Fix Incorrect Orientation of Digital PhotosAugust 8, 2026
EBT Weather is now available for Windows and LinuxMay 30, 2026
Node.js + Express: How to Block Requests by User-Agent HeadersJanuary 7, 2026
Vault 3 is Now Available for Windows on ARM Machines!December 13, 2025