Create a gist now

Instantly share code, notes, and snippets.

@qiao /ip.js
Created Jan 17, 2012

What would you like to do?
Node.js get client IP address
// snippet taken from http://catapulty.tumblr.com/post/8303749793/heroku-and-node-js-how-to-get-the-client-ip-address
function getClientIp(req) {
var ipAddress;
// The request may be forwarded from local web server.
var forwardedIpsStr = req.header('x-forwarded-for');
if (forwardedIpsStr) {
// 'x-forwarded-for' header may return multiple IP addresses in
// the format: "client IP, proxy 1 IP, proxy 2 IP" so take the
// the first one
var forwardedIps = forwardedIpsStr.split(',');
ipAddress = forwardedIps[0];
}
if (!ipAddress) {
// If request was not forwarded
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
};
@jiyinyiyong

This comment has been minimized.

Show comment Hide comment
@jiyinyiyong

jiyinyiyong Jul 28, 2012

半年过去接口似乎变了, 我打印了 req 然后发现下面这样可以用. 总算还好

getClientIp = (req) ->
  x_ip = req.headers['x-forwarded-for']
  unless x_ip? then x_ip = req.connection.remoteAddress
  x_ip

半年过去接口似乎变了, 我打印了 req 然后发现下面这样可以用. 总算还好

getClientIp = (req) ->
  x_ip = req.headers['x-forwarded-for']
  unless x_ip? then x_ip = req.connection.remoteAddress
  x_ip
@ghost

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Nov 10, 2012

function getClientAddress(request){ 
    with(request)
        return (headers['x-forwarded-for'] || '').split(',')[0] 
            || connection.remoteAddress
}

ghost commented Nov 10, 2012

function getClientAddress(request){ 
    with(request)
        return (headers['x-forwarded-for'] || '').split(',')[0] 
            || connection.remoteAddress
}
@talon

This comment has been minimized.

Show comment Hide comment
@talon

talon Feb 27, 2013

var getClientAddress = function (req) {
    return (req.headers['x-forwarded-for'] || '').split(',')[0] 
        || req.connection.remoteAddress;
};

Will that work? If so that is really concise and quite nice. I didn't know you could get so creative with the ||'s!

talon commented Feb 27, 2013

var getClientAddress = function (req) {
    return (req.headers['x-forwarded-for'] || '').split(',')[0] 
        || req.connection.remoteAddress;
};

Will that work? If so that is really concise and quite nice. I didn't know you could get so creative with the ||'s!

@mohamedmohsin

This comment has been minimized.

Show comment Hide comment
@mohamedmohsin

mohamedmohsin Oct 8, 2014

i am using express 3.4.8 i have enable trusted proxy by app.enable('trust proxy') ,my req.headers does not contain X-Forward-For

i am using express 3.4.8 i have enable trusted proxy by app.enable('trust proxy') ,my req.headers does not contain X-Forward-For

@ArnaudValensi

This comment has been minimized.

Show comment Hide comment
@ArnaudValensi

ArnaudValensi Mar 23, 2015

The header can be x-forwarded-for or X-Forwarded-For, so:

var getClientIp = function(req) {
    return (req.headers["X-Forwarded-For"] ||
            req.headers["x-forwarded-for"] ||
            '').split(',')[0] ||
           req.client.remoteAddress;
};

The header can be x-forwarded-for or X-Forwarded-For, so:

var getClientIp = function(req) {
    return (req.headers["X-Forwarded-For"] ||
            req.headers["x-forwarded-for"] ||
            '').split(',')[0] ||
           req.client.remoteAddress;
};
@aderbas

This comment has been minimized.

Show comment Hide comment
@aderbas

aderbas Nov 5, 2015

var ip = req.headers["X-Forwarded-For"] || req.connection.remoteAddress;

Always returns the host IP and not the client IP in my case.

aderbas commented Nov 5, 2015

var ip = req.headers["X-Forwarded-For"] || req.connection.remoteAddress;

Always returns the host IP and not the client IP in my case.

@LandonPowell

This comment has been minimized.

Show comment Hide comment
@LandonPowell

LandonPowell Dec 20, 2015

X-Forwarded-For? You're checking easily spoofed headers to get an IP?

X-Forwarded-For? You're checking easily spoofed headers to get an IP?

@krynomore

This comment has been minimized.

Show comment Hide comment
@krynomore

krynomore Dec 20, 2015

If you install gentoo you don't need all of this

If you install gentoo you don't need all of this

@mafia-007

This comment has been minimized.

Show comment Hide comment
@mafia-007

mafia-007 Apr 30, 2017

hi
Can we to find the user's IP telegram?

hi
Can we to find the user's IP telegram?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment