Node.js
I’ve heard a lot about node.js recently so I looked after it a bit. At the homepage of the project you can find a one-hour long video presentation of the project.
Node.js is a set of libraries on top of Google’s V8 Javascript engine, which is used in Chrome. V8 is a very high performance virtual machine. Node.js uses the greatness of V8 to do networking things. The focus is on doing networking correctly.
Examples
Hello World:
setTimeout(function() {
console.log('world');
}, 2000)
console.log('hello');
It registers that the first function must be executed in 2 sec. and it goes on. It prints ‘hello’, then ‘world’ 2 sec. later. One very important thing: in node.js there is no blocking (like sleep()). The program exits when there is nothing else left to do.
Simple web server:
var http = require('http');
var s = http.createServer(function(req, res) {
res.writeHead(200, {'content-type' : 'text/plain'});
res.write("hello\n");
setTimeout(function() {
res.write("world\n");
res.end();
}, 2000)
});
s.listen(8000);
All these examples are from the introductory video. In the video there is also a TCP server. He also shows a simple chat server via TCP.
Installation
Under Ubuntu I already had a “node” command. According to “man node” it was something different so I removed it (sudo apt-get remove node).
Download the source from the HQ, extract it, then configure, make, sudo make install.
Start it with the command “node” and you get a prompt. Execute a script with “node file.js”.
Further reading
- What is node.js? @ SO
- npm, a package manager for node
- What is Node.js?
- Node.js is genuinely exciting
- Understanding node.js
- Learning Server-Side JavaScript with Node.js
- Node.js Step by Step: Introduction
- Node.js is Cancer (Ted Dziuba doesn’t like Node.js)