Created
July 6, 2012 01:36
-
-
Save yelkhatib/3057491 to your computer and use it in GitHub Desktop.
Getting Client information in NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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