Home > javascript > Node.js

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

About these ads
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 70 other followers

%d bloggers like this: