Getting Client information in NodeJS

To get information about your client in Node.JS (because it is slightly different than in PHP).

Here’s an example snippet from onRequest that you could use in your server start function.

(Gist here.)

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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s