+ All Categories
Home > Documents > node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy...

node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy...

Date post: 01-Sep-2019
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
50
node.js A quick tour 15.02.2011 (v4)
Transcript
Page 1: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

node.jsA quick tour

15.02.2011 (v4)

Page 2: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

About

• Felix Geisendörfer

• 23 years

• Berlin, Germany

@felixge

Page 3: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Core Contributor

&

Module Author

node-mysql node-formidable

Page 4: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

File uploading & processing as an infrastructure service for web & mobile applications.

Page 5: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Audience?

Page 6: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Node's goal is to provide an easy way to build scalable network programs.

Page 7: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Node.js

• Created by Ryan Dahl

• Google’s V8 JavaScript engine (no DOM)

• Module system, I/O bindings, Common Protocols

Page 8: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Installing

$ git clone \git://github.com/ry/node.git$ cd node$ ./configure$ make install

Page 9: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Hello World

$ cat test.jsconsole.log('Hello World');

$ node test.jsHello World

Page 10: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Ingredients

V8

libev

http_parser

c-ares

libeio

Page 11: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Philosophy

• Just enough core-library to do I/O

• Non-blocking

• Close to the underlaying system calls

Page 12: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Non-blocking I/O

Page 13: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

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 14: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

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 15: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Non-Blocking I/O

• Offload most things to the kernel and poll for changes (select, epoll, kqueue, etc.)

• Thread pool for anything else

Page 16: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Single Threadedvar a = [];function f() { a.push(1); a.push(2);}

setTimeout(f, 10);setTimeout(f, 10);

Page 17: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

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 18: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

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 19: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

API Overview

Page 20: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

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 21: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

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 22: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

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 23: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

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 24: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

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 25: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

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 26: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

And much more

• UDP

• Crypto

• Assert

• Buffer

• Script

• EcmaScript5

...

Page 27: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Suitable Applications

Page 28: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Suitable Applications

• Singe-page apps

• Real time

• Crawlers

Page 29: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

More Applications

• Async chaining of unix tools

• Streaming

• File uploading

Page 30: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Interesting projects

Page 31: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Package management

Page 32: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Web Frameworks

• Express.js (Sinatra clone)

• Fab.js (Mind-bending & awesome)

Page 33: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

DOM

• node-htmlparser

• jquery

• node.io

Page 34: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

WebSockets

Socket.IO

Page 35: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

1171 modules in npm

Page 36: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Ready for production?

Page 37: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Ready for production?

• 0.4 was released last week

• API has settled down for most parts

• Very few bugs, but YMMV

Page 38: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Things that suck

Page 39: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

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 40: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Community

Page 42: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Community

• Mailing list (nodejs, nodejs-dev)

• IRC (#node.js)

Page 43: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Hosting

Page 45: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Bonus Slides

Page 46: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Speed

Page 47: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

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 48: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Speed

100 concurrent clients1 megabyte response

node 822 req/secnginx 708thin 85mongrel 4

(bigger is better)by Ryan Dahl

Page 49: node - felixge.s3.amazonaws.comfelixge.s3.amazonaws.com/11/nodejs-a-quick-tour-v4.pdf · Philosophy • Just enough core-library to do I/O • Non-blocking • Close to the underlaying

Speed

• NGINX peaked at 4mb of memory

• Node peaked at 60mb

• Very special circumstances

by Ryan Dahl


Recommended