2. WHAT IS NODE.JS
“Node has a clear purpose: provide an easy
way to build scalable network programs. It is
not a tool for every problem. Do not write a ray
tracer with Node. Do not write a web browser
with Node. Do however reach for Node if
tasked with writing a DNS server, DHCP
server, or even a video encoding server.”
Ryan Dahl
3. NODE.JS
● server-side javascript environment
● asynchronous event-driven model
● based on Google's V8 javascript engine
● open source, controlled by Joyent
● well documented
● lot of 3rd party modules
4. ASYNCHRONOUS?
● like Python's Twisted or Ruby's
EventMachine
● the event loop is hidden from the user
● non-blocking I/O (network, file, etc.)
● developers should think async
5. ASYNCHRONOUS!
mysql_select_db('db');
$result = mysql_query("SELECT * FROM table");
while ($row = mysql_fetch_array($result)) {
echo $row['data'];
}
echo "finished";
var mysql = require('mysql'),
client = mysql.createClient({
user: 'user',
password: 'password'
});
client.query('USE db');
client.query('SELECT * FROM table',
function selectCb(err, result, fields) {
console.log(result);
}
);
console.log("finished");
6. NO THREADS
● a node.js application is single threaded
● child_process.fork()
● I/O doesn't block, but you can:
for (var i = 0; i < 10000; i++) {
heavy_calculate_PI();
}
for (var i = 0; i < 10000; i++) {
setTimeout(heavy_calculate_PI, 0);
}
7. MODULES
● CommonJS Modules proposal
var hello = exports;
hello.say = function (name) { console.log("Hello "+name); }
var hello = require('./hello');
hello.say('Sanyi'); // "Hello Sanyi!"
● vs. AMD [Asynchronous Module Definition]
8. MODULES
● npm - node package manager
● more then 1000 listed on the wiki
● ~5000 repositories on github related to node.js
Web frameworks (MVC like fws, static file servers,
middlewares), database drivers (mysql, pgsql, sqlite,
mongodb, ...), templating, css engines, CMSs, SSL/crypto
fws, SMTP, TCP/IP, RPC, web socket, message queues,
class systems, testing fws, parsers (json, xml, command
line, parser generators), debugging utilities, compression,
graphics, payment gateways, i18n/l10n, coffeescript, full
text search, ...
9. HTTP MODULE
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");
console.log('Server running at http://127.0.0.1:1337/');
$ node hello.js
Server running at http://127.0.0.1:1337/
10. SPEED
● Google V8 (used in Chrome as well), JIT
● may perform better then not optimized C++
(performance test)
● ~25 times faster than PHP (~10* CPython,
~2.5* PyPy)
● http module outperforms apache, sometimes
even nginx
● typical setup: multiple node.js instances
running behind an nginx (or node.js) proxy
12. jsdom
● DOM implementation for node.js
● almost everything works
● YUI 3, jQuery, ...
● unit testing web applications
● rendering and manipulating content on
server side
13. HOSTING
● easy to install
○ very few dependecies
○ ./configure && make
● easy to manage
○ no need for webserver
● Lot of managed node.js hosting providers:
Heroku, no.de, CloudFoundry, dotCloud, ...
14. EXAMPLE PROJECTS
● Calipso - CMS based on node.js
● 64squares - Real time chess on facebook
● Hummingbird - real time visitor statistics
● Kraken.io - image optimizer
● Word2 - MMO word game
● ...