nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js...

36
node.js A quick tour (V3) 07.10.2010

Transcript of nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js...

Page 1: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

node.jsA quick tour (V3)

07.10.2010

Page 2: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

About

Contributor Co-founder

node.js driver node.js file uploads

Felix Geisendörfer formidable

Page 3: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Audience?

Page 4: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

JavaScript?

Page 5: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

What is node?

Page 6: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Server side JavaScript

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

Page 7: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Server side JavaScript

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

$ node test.jsHello World

Page 8: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Server side JavaScript

$ node> console.log('Hello World');Hello World>

Page 9: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Ingredients

V8

libev

http_parser

c-ares

libeio

Page 10: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Philosophy

• Just enough core-library to do I/O

• Non-blocking

• Close to the underlaying system calls

Page 12: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Concurrency Model

Page 13: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Non-blocking I/O

var fs = require('fs');

fs.readFile('test.txt', function(err, data) { if (err) throw err;

console.log('Read file: ', data);});

console.log('Reading file ...');

Page 14: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Single Threaded

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

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

Page 15: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

API Overview

Page 16: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

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 17: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Child processes$ cat child.jsvar cmd = 'echo hello; sleep 1; echo world;', spawn = require('child_process').spawn, 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 18: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

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 19: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Tcp Server$ cat tcp.jsvar tcp = require('tcp');tcp.createServer(function(socket) { socket.on('connect', function() { socket.write("Hi, How Are You?\n> "); }); socket.on('data', function(data) { socket.write(data); });}).listen(4000);

$ nc localhost 4000Hi, How Are You?> Great!Great!

Page 20: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

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 21: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

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 22: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

And much more

• UDP

• Crypto

• Assert

• Buffer

• Script

• EcmaScript5

...

Page 23: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Suitable Applications

• Web frameworks

• Real time

• Crawlers

Page 24: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

More Applications

• Process monitoring

• File uploading

• Streaming

Page 25: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Interesting projects

Page 26: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Package management

Page 27: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Web Frameworks

• Express.js (Sinatra clone)

• Fab.js (Mind-bending & awesome)

Page 28: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

DOM

• jsdom

• node-htmlparser

• apricot

Page 29: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

WebSockets

Socket.IO

Page 30: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Protocol parsers

• node-formidable

• node-mysql

Page 31: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

....

Page 32: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Limitations

• Weak SSL support

• Weak Windows support

• 1GB Heap limit on x64 (V8)

Page 33: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable

Hosting

Page 35: nodefelixge.s3.amazonaws.com/10/nodejs-a-quick-tour-v3.pdf · About Contributor Co-founder node.js driver node.js file uploads Felix Geisendörfer formidable