Skip to content

Instantly share code, notes, and snippets.

@yelkhatib
Created July 6, 2012 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yelkhatib/3057491 to your computer and use it in GitHub Desktop.
Save yelkhatib/3057491 to your computer and use it in GitHub Desktop.
Getting Client information in NodeJS
function onRequest(request, response) {
var clientIPaddr = null,
clientProxy = null;
// is client going through a proxy?
if (request.headers['via']) { // yes
clientIPaddr = request.headers['x-forwarded-for'];
clientProxy = request.headers['via'];
} else { // no
clientIPaddr = request.connection.remoteAddress;
clientProxy = "none";
}
var pathname = url.parse(request.url).pathname;
if (pathname!="/favicon.ico") {
console.log(">> Request for "+pathname);
console.log(">>> Client : "+request.headers['user-agent']);
console.log(">>> IP address "+clientIPaddr+" via proxy "+clientProxy);
}
// rest of request handling code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment