Node.js
February 2, 2010
Node.js is a relatively new project which implements a server side non blocking framework in JavaScript, which runs on Google's V8 JavaScript engine. It only works on Linux/Mac OSX type machines at the moment
To install
Here's how to install from source:
$ git clone git://github.com/ry/node.git $ ./configure $ make $ sudo make install
To test
Create a helloworld.js file with these contents:
var sys = require('sys'); var http = require('http'); http.createServer(function(request, response){ response.sendHeader(200, {'Content-Type': 'text/html'}); response.sendBody('<h1>Hello World</h1>'); response.finish(); }).listen(8080); sys.puts('Server running at http://127.0.0.1:8080/');
Run this with node helloworld.js
. Test it from a browser.
Volume Testing
This stuff is insanely fast. Testing using Apache Bench on my quad core machine:
$ ab -n 1000 -c 100 'http://127.0.0.1:8080/'
results in a throughput of 6084.24 requests per second (1000 requests, 100 concurrent). Testing again with 10,000 requests gives 7906.68 request per second.
Processing GET parameters
Here's an example of some server code that will receive two query parameters and then print out their values in the returned page. In this case, whatever is passed through as the q
and f
parameters will be directly printed in the page, along with some other details.
var sys = require('sys'); var http = require('http'); var url = require('url'); http.createServer(function(request, response){ lVars = url.parse(request.url, true); response.sendHeader(200, {'Content-Type': 'text/html'}); response.sendBody('<h1>Hello World</h1>'); response.sendBody('<table>'); response.sendBody('<tr><td>href</td><td>' + lVars.href + '</td></tr>'); response.sendBody('<tr><td>pathname</td><td>' + lVars.pathname + '</td></tr>'); response.sendBody('<tr><td>search</td><td>' + lVars.search + '</td></tr>'); if (lVars.query){ response.sendBody('<tr><td>query q</td><td>' + lVars.query.q + '</td></tr>'); response.sendBody('<tr><td>query f</td><td>' + lVars.query.f + '</td></tr>'); } response.sendBody('</table>'); response.finish(); }).listen(8080); sys.puts('Server running at http://127.0.0.1:8080/');
Call this at http://127.0.0.1:8080/?q=123&f=blah and you'll get the following output:
href /?q=123&f=blah pathname / search ?q=123&f=blah query q 123 query f blah
HTTP Client
This code is an http client that will request the homepage from www.drumcoder.co.uk.
var http = require('http'); var sys = require('sys'); function read(callback) { var connection = http.createClient(80, "www.drumcoder.co.uk"); var request = connection.request("GET", "/", {'host': 'www.drumcoder.co.uk'}); request.addListener("response", function(response) { var responseBody = ""; response.setBodyEncoding("utf8"); response.addListener("data", function(chunk) { responseBody += chunk; }); response.addListener("end", function() { callback(responseBody); }) }); request.end(); } function print_results(data) { sys.puts(data); } read(print_results);
See Also
-
http://simonwillison.net/2009/Nov/23/node/. Simon has created a project to map Django's URL routing onto this framework.