Nodejs - A quick tour (v5)
-
Upload
felix-geisendoerfer -
Category
Technology
-
view
2.763 -
download
3
description
Transcript of Nodejs - A quick tour (v5)
![Page 1: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/1.jpg)
node.jsA quick tour
15.02.2011 (v4)
![Page 2: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/2.jpg)
About
• Felix Geisendörfer
• Berlin, Germany
• Debuggable Ltd
![Page 3: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/3.jpg)
@felixge
![Page 4: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/4.jpg)
File uploading & processing as an infrastructure service for web & mobile applications.
![Page 5: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/5.jpg)
Core Contributor
&
Module Author
node-mysql node-formidable
![Page 6: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/6.jpg)
Audience?
![Page 7: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/7.jpg)
Server-side JavaScript
![Page 8: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/8.jpg)
Server-side JavaScript
• Netscape LiveWire (1996)
• Rhino (1997)
• 50+ plattforms since then
![Page 9: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/9.jpg)
What was the problem?
• Slow engines
• Lack of interest in JS until ~2005
• Better competing plattforms
![Page 10: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/10.jpg)
Why JavaScript?3 Reasons
![Page 11: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/11.jpg)
#1 - The good parts
![Page 12: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/12.jpg)
#2 - JS Engine Wars
V8 (Chrome)
Chakra (IE9)
SpiderMonkey (Firefox) JavaScriptCore (Safari)
Carakan (Opera)
![Page 13: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/13.jpg)
#3 - No I/O in the core
![Page 14: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/14.jpg)
Node's goal is to provide an easy way to build scalable network programs.
![Page 15: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/15.jpg)
Node.js
• Created by Ryan Dahl
• Google’s V8 JavaScript engine (no DOM)
• Module system, I/O bindings, Common Protocols
![Page 16: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/16.jpg)
Installing
$ git clone \git://github.com/ry/node.git$ cd node$ ./configure$ make install
![Page 17: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/17.jpg)
Hello World
$ cat test.jsconsole.log('Hello World');
$ node test.jsHello World
![Page 18: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/18.jpg)
Ingredients
V8
libev
http_parser
c-ares
libeio
![Page 19: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/19.jpg)
Philosophy
• Just enough core-library to do I/O
• Non-blocking
• Close to the underlaying system calls
![Page 20: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/20.jpg)
Non-blocking I/O
![Page 21: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/21.jpg)
Blocking I/O
var a = db.query('SELECT A');console.log('result a:', a);
var b = db.query('SELECT B');console.log('result b:', b);
Time = SUM(A, B)
![Page 22: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/22.jpg)
Non-Blocking I/O
db.query('SELECT A', function(result) { console.log('result a:', result);});
db.query('SELECT B', function(result) { console.log('result b:', result);});
Time = MAX(A, B)
![Page 23: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/23.jpg)
Non-Blocking I/O
• Offload most things to the kernel and poll for changes (select, epoll, kqueue, etc.)
• Thread pool for anything else
![Page 24: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/24.jpg)
Single Threadedvar a = [];function f() { a.push(1); a.push(2);}
setTimeout(f, 10);setTimeout(f, 10);
![Page 25: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/25.jpg)
Single Threaded
• [1, 1, 2, 2] ?
• [1, 2, 1, 2] ?
• BAD ACCESS ?
var a = [];function f() { a.push(1); a.push(2);}
setTimeout(f, 10);setTimeout(f, 10);
![Page 26: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/26.jpg)
Single Threaded
• [1, 1, 2, 2]
•[1, 2, 1, 2]
• BAD ACCESS
var a = [];function f() { a.push(1); a.push(2);}
setTimeout(f, 10);setTimeout(f, 10);
![Page 27: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/27.jpg)
API Overview
![Page 28: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/28.jpg)
CommonJS Modules$ cat hello.jsexports.world = function() { return 'Hello World';};
$ cat main.jsvar hello = require('./hello');console.log(hello.world());
$ node main.jsHello World
![Page 29: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/29.jpg)
Child processes$ cat child.jsvar spawn = require('child_process').spawn;var cmd = 'echo hello; sleep 1; echo world;';
var child = spawn('sh', ['-c', cmd]);child.stdout.on('data', function(chunk) { console.log(chunk.toString());});
$ node child.js"hello\n"# 1 sec delay"world\n\n"
![Page 30: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/30.jpg)
Http Server$ cat http.jsvar http = require('http');http.createServer(function(req, res) { setTimeout(function() { res.writeHead(200); res.end('Thanks for waiting!'); }, 1000);}).listen(4000);
$ curl localhost:4000# 1 sec delayThanks for waiting!
![Page 31: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/31.jpg)
Tcp Server$ cat tcp.jsvar tcp = require('tcp');tcp.createServer(function(socket) { socket .on('connect', function() { socket.write("Hi, How Are You?\n> "); }) .on('data', function(data) { socket.write(data); });}).listen(4000);
$ nc localhost 4000Hi, How Are You?> Great!Great!
![Page 32: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/32.jpg)
DNS
$ cat dns.jsvar dns = require('dns');dns.resolve('nodejs.org', function(err, addresses) { console.log(addresses);});
$ node dns.js[ '8.12.44.238' ]
![Page 33: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/33.jpg)
Watch File$ cat watch.jsvar fs = require('fs');fs.watchFile(__filename, function() { console.log('You changed me!'); process.exit(0);});
$ node watch.js# edit watch.jsYou changed me!
![Page 34: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/34.jpg)
And much more
• UDP
• Crypto
• Assert
• Buffer
• Script
• EcmaScript5
...
![Page 35: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/35.jpg)
Suitable Applications
![Page 36: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/36.jpg)
Suitable Applications
• Singe-page apps
• Real time
• Crawlers
![Page 37: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/37.jpg)
More Applications
• Async chaining of unix tools
• Streaming
• File uploading
![Page 38: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/38.jpg)
Interesting projects
![Page 39: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/39.jpg)
Package management
![Page 40: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/40.jpg)
Web Frameworks
• Express.js (Sinatra clone)
• Fab.js (Mind-bending & awesome)
![Page 41: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/41.jpg)
DOM
• node-htmlparser
• jquery
• node.io
![Page 42: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/42.jpg)
WebSockets
Socket.IO
![Page 43: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/43.jpg)
1171 modules in npm
![Page 44: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/44.jpg)
Ready for production?
![Page 45: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/45.jpg)
Ready for production?
• 0.4.2 was released on March 3rd
• API has settled down for most parts
• Very few bugs, but YMMV
![Page 46: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/46.jpg)
Things that suck
![Page 47: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/47.jpg)
Things that suck• Stack traces are lost at the event loop
boundary
• Utilizing multiple cores requires multiple processes
• V8: 1.9 GB heap limit / GC can be problematic
![Page 48: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/48.jpg)
Community
![Page 49: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/49.jpg)
Benevolent Dictator For Life
Ryan Dahl
![Page 50: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/50.jpg)
Community
• Mailing list (nodejs, nodejs-dev)
• IRC (#node.js)
![Page 51: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/51.jpg)
Hosting
![Page 53: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/53.jpg)
Bonus Slides
![Page 54: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/54.jpg)
Speed
![Page 55: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/55.jpg)
Speed
var http = require(’http’)var b = new Buffer(1024*1024);
http.createServer(function (req, res) { res.writeHead(200); res.end(b);}).listen(8000);
by Ryan Dahl
![Page 56: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/56.jpg)
Speed
100 concurrent clients1 megabyte response
node 822 req/secnginx 708thin 85mongrel 4
(bigger is better)by Ryan Dahl
![Page 57: Nodejs - A quick tour (v5)](https://reader034.fdocuments.in/reader034/viewer/2022051323/54b794c84a7959db528b4b4f/html5/thumbnails/57.jpg)
Speed
• NGINX peaked at 4mb of memory
• Node peaked at 60mb
• Very special circumstances
by Ryan Dahl