Node.js quick intro
Node.js
Platform for scalable network applications
servers and clients
Uses event driven, non-blocking I/O
epoll, kqueue, /dev/poll, or select
process never blocks, no deadlocks
internal event loop
Event driven platform
Non-blocking - not waiting for things to finish
Uses asynchronous platform-specific API’s
“Single-threaded” - sequential task execution
Runs synchronous API’s in threads
Node.js architecture
Based on V8
JavaScript Virtual
Machine (engine)
+ standard library
Google Chrome uses
V8 too
Event loop illustration
// event loop
while (true) {
lock (queue) {
var tickEvents = copy(queue);
queue.empty();
}
for (var i = 0; i < tickEvents.length; i++) {
InvokeJSFunction(tickEvents[i]);
}
}
// thread-safe event pushing
lock (queue) {
queue.push(event);
}
// io call
fs.readFile(“pwd”, function(e, d){
console.log(“file data:”, d);
});
Callbacks
var fs = require(“fs”);
var cb = function(err, data){
if (err) throw err;
console.log(“File contents: ” + data);
}
fs.readFile(“pwd1”, cb);
fs.readFile(“pwd2”, cb);
Http server app
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).listen(1337, "127.0.0.1");
Node.js launching
$ echo ‘console.log(“hey there”)’ | node
$ node app.js
Workers event loop
That was quick Node.js introduction
Thanks

Node.js quick intro

  • 1.
  • 2.
    Node.js Platform for scalablenetwork applications servers and clients Uses event driven, non-blocking I/O epoll, kqueue, /dev/poll, or select process never blocks, no deadlocks internal event loop
  • 3.
    Event driven platform Non-blocking- not waiting for things to finish Uses asynchronous platform-specific API’s “Single-threaded” - sequential task execution Runs synchronous API’s in threads
  • 4.
    Node.js architecture Based onV8 JavaScript Virtual Machine (engine) + standard library Google Chrome uses V8 too
  • 5.
    Event loop illustration //event loop while (true) { lock (queue) { var tickEvents = copy(queue); queue.empty(); } for (var i = 0; i < tickEvents.length; i++) { InvokeJSFunction(tickEvents[i]); } } // thread-safe event pushing lock (queue) { queue.push(event); } // io call fs.readFile(“pwd”, function(e, d){ console.log(“file data:”, d); });
  • 6.
    Callbacks var fs =require(“fs”); var cb = function(err, data){ if (err) throw err; console.log(“File contents: ” + data); } fs.readFile(“pwd1”, cb); fs.readFile(“pwd2”, cb);
  • 7.
    Http server app varhttp = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(1337, "127.0.0.1");
  • 8.
    Node.js launching $ echo‘console.log(“hey there”)’ | node $ node app.js
  • 9.
  • 10.
    That was quickNode.js introduction Thanks