Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, enabling its use for real-time web applications. Node.js allows JavaScript code to run on the server, facilitating the creation of fast and scalable network applications like web servers through its asynchronous and event-driven architecture.
1. Node.js
“Because nothing blocks, less-than-expert
programmers are able to develop fast systems.”
http://nodejs.org/#about
04/2010 Rocco Georgi, PavingWays Ltd.
2. Node.js
written in C and JavaScript
I/O framework
evented
non-blocking
server-side JavaScript
CommonJS module system
using V8
04/2010 Rocco Georgi, PavingWays Ltd.
3. written in C
./configure
make
make install
#> node myfile.js
04/2010 Rocco Georgi, PavingWays Ltd.
5. evented
var tcp = require('tcp');
var server = tcp.createServer(function (socket) {
socket.setEncoding("utf8");
event handlers socket.addListener("connect", function () {
socket.send("hellorn");
for anything });
socket.addListener("receive", function (data) {
event loop });
socket.send(data);
no threads socket.addListener("eof", function () {
socket.send("goodbyern");
socket.close();
});
});
server.listen(7000, "localhost");
04/2010 Rocco Georgi, PavingWays Ltd.
6. non-blocking
I/O is never blocking anything
callbacks on anything
everything in a single thread + loop
very small memory footprint
04/2010 Rocco Georgi, PavingWays Ltd.
7. server-side JS
V8 engine
CommonJS: file = module
// this loads circle.js and the sys object
var circle = require('./circle');
var sys = require('sys');
// use the sys and circle object
sys.puts('circle area at radius 4 = ' + circle.area(4));
no window object
04/2010 Rocco Georgi, PavingWays Ltd.
8. Benefits
low memory footprint per connection
high concurrency
“hanging” requests -> Comet
custom TCP/HTTP servers
active module community
e.g. WebSockets server
04/2010 Rocco Georgi, PavingWays Ltd.