HTTP-request header from node.js

The message header of requests and responses in the Hypertext Transfer Protocol define the operating parameters of an HTTP transaction.

Say if you want to do a language detection in the browser : One must be aware that browser settings don't actually affect the navigator.language property that is obtained via javascript, but they do affect is the HTTP 'Accept-Language' header, but this value is not available through javascript at all!

So we need some server side help, why not use javascript itself on the server side?

If you have still not got node on your server, do the below to get up and running :

mkdir ~/local
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc

sudo apt-get install libssl-dev

git clone git://
cd node
git checkout v0.6.6

./configure --prefix=~/local
make install
cd ..

git clone git://
cd npm
make install


After, which you must have node and npm on your box. The below is a simple script that return the HTTP-request header :

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});

So, say for a chrome browser on Debian machine, here is what the JSON response will be :

    "host": "",
    "connection": "keep-alive",
    "user-agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "accept-encoding": "gzip,deflate,sdch",
    "accept-language": "en-US,en;q=0.8",
    "accept-charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3"

So, there was some fun getting the HTTP-headers, do let me know your way!

Share this