Skip to content

Instantly share code, notes, and snippets.

@qiao
Created January 17, 2012 11:27
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save qiao/1626318 to your computer and use it in GitHub Desktop.
Save qiao/1626318 to your computer and use it in GitHub Desktop.
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;
};
@mafia-007
Copy link

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

Copy link

ghost commented May 31, 2018

it is hacked. no use

@Wengiel31
Copy link

What should I pass as the "req" parameter?

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