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://github.com/joyent/node.git cd node git checkout v0.6.6 ./configure --prefix=~/local make install cd .. git clone git://github.com/isaacs/npm.git cd npm make install bash
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'}); res.end(JSON.stringify(req.headers)); }).listen(8086,'0.0.0.0');
So, say for a chrome browser on Debian machine, here is what the JSON response will be :
{ "host": "0.0.0.0:8080", "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!
Recent blog posts
- watir-webdriver web inspector
- gem list to gemfile
- Packing ruby2.0 on debian.
- Made it into The Guinness Book!
- to_h in ruby 2.0
- Filter elements by pattern jQuery.
- Better HTML password fields for mobile ?
- Grayscale image when user offline
- nth-child CSS pseudo-class Christmas colors
- EventEmitter in nodejs