1. Download
  2. ChangeLog
  3. Build
  4. About
  5. Links
  6. Contributing
  7. Documentation

Evented I/O for V8 javascript.

An example of a web server written with Node which responds with "Hello World" after waiting two seconds:

var sys = require('sys'), 
   http = require('http');
http.createServer(function (req, res) {
  setTimeout(function () {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write('Hello World');
    res.close();
  }, 2000);
}).listen(8000);
sys.puts('Server running at http://127.0.0.1:8000/');

To run the server, put the code into a file example.js and execute it with the node program

% node example.js
Server running at http://127.0.0.1:8000/

Here is an example of a simple TCP server which listens on port 7000 and echos whatever you send it:

var tcp = require('tcp');
var server = tcp.createServer(function (socket) {
  socket.setEncoding("utf8");
  socket.addListener("connect", function () {
    socket.write("hello\r\n");
  });
  socket.addListener("data", function (data) {
    socket.write(data);
  });
  socket.addListener("end", function () {
    socket.write("goodbye\r\n");
    socket.close();
  });
});
server.listen(7000, "localhost");

See the API documentation for more examples.

Download

git repo

2010.03.12 node-v0.1.32.tar.gz

Build

Node eventually wants to support all POSIX operating systems (including Windows with MinGW) but at the moment it is only being tested on Linux, Macintosh, and FreeBSD. The build system requires Python 2.4 or better. V8, on which Node is built, supports only IA-32 and ARM processors. V8 is included in the Node distribution. To use TLS, GnuTLS and libgpg-error are required. There are no other dependencies.

./configure
make
make install

Then have a look at the API documentation.

To run the tests

make test

About

Node's goal is to provide an easy way to build scalable network programs. In the above example, the two second delay does not prevent the server from handling new requests. Node tells the operating system (through epoll, kqueue, /dev/poll, or select) that it should be notified when the 2 seconds are up or if a new connection is made—then it goes to sleep. If someone new connects, then it executes the callback, if the timeout expires, it executes the inner callback. Each connection is only a small heap allocation.

This is in contrast to today's more common concurrency model where OS threads are employed. Thread-based networking is relatively inefficient and very difficult to use. Node will show much better memory efficiency under high-loads than systems which allocate 2mb thread stacks for each connection. Furthermore, users of Node are free from worries of dead-locking the process—there are no locks. Almost no function in Node directly performs I/O, so the process never blocks. Because nothing blocks, less-than-expert programmers are able to develop fast systems.

Node is similar in design to and influenced by systems like Ruby's Event Machine or Python's Twisted. Node takes the event model a bit further—it presents the event loop as a language construct instead of as a library. In other systems there is always a blocking call to start the event-loop. Typically one defines behavior through callbacks at the beginning of a script and at the end starts a server through a blocking call like EventMachine::run(). In Node there is no such start-the-event-loop call. Node simply enters the event loop after executing the input script. Node exits the event loop when there are no more callbacks to perform. This behavior is like browser javascript—the event loop is hidden from the user.

HTTP is a first class protocol in Node. Node's HTTP library has grown out of the author's experiences developing and working with web servers. For example, streaming data through most web frameworks is impossible. Node attempts to correct these problems in its HTTP parser and API. Coupled with Node's purely evented infrastructure, it makes a good foundation for web libraries or frameworks.

But what about multiple-processor concurrency? Threads are necessary to scale programs to multi-core computers. Processes are necessary to scale to multi-core computers, not memory-sharing threads. The fundamentals of scalable systems are fast networking and non-blocking design—the rest is message passing. In future versions, Node will be able to fork new processes (using the Web Workers API ), but this is something that fits well into the current design.

See also: slides from jsconf.

Contributing

Patches are welcome. The process is simple:

git clone git://github.com/ry/node.git
cd node
(make your changes)
./configure --debug
make test-all # Check your patch with both debug and release builds
git commit -m "Good description of what your patch does"
git format-patch HEAD^

The best way for your patch to get noticed is to submit it to the mailing list in form of a gists or file attachement.

You should ask the mailing list if a new feature is wanted before working on a patch.