Node.js - A Quick Tour

28,252 views
28,052 views

Published on

Slides for my talk at the Berlin JavaScript user group on Jan 12th, 2010.

Published in: Technology, Education
2 Comments
53 Likes
Statistics
Notes
  • Maybe better in pastebin: http://pastebin.com/vcPBnc2p
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Updated code:

    var http = require('http'),
    messages = [];

    http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type' : 'text/plain'});
    if(req.url == '/'){
    res.end(messages.join('\n'));
    } else if (req.url !== '/favicon.ico'){
    messages.push(decodeURIComponent(req.url.substr(1)));
    res.end('ok!');
    }
    }).listen(4000);
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
28,252
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
665
Comments
2
Likes
53
Embeds 0
No embeds

No notes for slide

Node.js - A Quick Tour

  1. 1. node.js A quick tour by Felix Geisendörfer
  2. 2. Introduction
  3. 3. Why? Node's goal is to provide an easy way to build scalable network programs. -- nodejs.org
  4. 4. How? Keep slow operations from blocking other operations.
  5. 5. Traditional I/O var data = file.read('file.txt'); doSomethingWith(data); Something is not right here
  6. 6. Traditional I/O var data = file.read('file.txt'); // zzzZZzzz FAIL! doSomethingWith(data); Don’t waste those cycles!
  7. 7. Async I/O file.read('file.txt', function(data) { doSomethingWith(data); }); WIN ! doSomethingElse(); No need to wait for the disk, do something else meanwhile!
  8. 8. The Present
  9. 9. CommonJS Modules hello.js exports.world = function() { return 'Hello World'; }; main.js var hello = require('./hello'); var sys = require('sys'); sys.puts(hello.world()); $ node main.js Hello World
  10. 10. Child processes child.js var child = process.createChildProcess('sh', ['-c', 'echo hello; sleep 1; echo world;']); child.addListener('output', function (chunk) { p(chunk); }); $ node child.js "hellon" # 1 sec delay "worldn" null
  11. 11. Http Server var http = require('http'); http.createServer(function(req, res) { setTimeout(function() { res.sendHeader(200, {'Content-Type': 'text/plain'}); res.sendBody('Thanks for waiting!'); res.finish(); }, 1000); }).listen(4000); $ curl localhost:4000 # 1 sec delay Thanks for waiting!
  12. 12. Tcp Server var tcp = require('tcp'); tcp.createServer(function(socket) { socket.addListener('connect', function() { socket.send("Hi, How Are You?n> "); }); socket.addListener('receive', function(data) { socket.send(data); }); }).listen(4000); $ nc localhost 4000 Hi, How Are You? > Great! Great!
  13. 13. DNS dns.js var dns = require('dns'); dns.resolve4('nodejs.org') .addCallback(function(r) { p(r); }); $ node dns.js [ "97.107.132.72" ]
  14. 14. Watch File watch.js process.watchFile(__filename, function() { puts('You changed me!'); process.exit(); }); $ node watch.js # edit watch.js You changed me!
  15. 15. ECMAScript 5 • Getters / setters var a = {}; a.__defineGetter__('foo', function() { return 'bar'; }); puts(a.foo); • Array: filter, forEach, reduce, etc. • JSON.stringify(), JSON.parse()
  16. 16. There is only 1 thread file.read('file.txt', function(data) { // Will never fire }); while (true) { // this blocks the entire process } Good for conceptual simplicity Bad for CPU-bound algorithms
  17. 17. The Future
  18. 18. Web workers • Multiple node processes that do interprocess communication • CPU-bound algorithms can run separately • Multiple CPU cores can be used efficiently
  19. 19. Move C/C++ stuff to JS • Simplifies the code base • Makes contributions easier • Low-level bindings = more flexibility
  20. 20. Better Socket Support • Support for unix sockets, socketpair(), pipe() • Pass sockets between processes " load balance requests between web workers • Unified socket streaming interfaces
  21. 21. Even more ... • Connecting streams var f = file.writeStream('/tmp/x.txt'); connect(req.body, f); • Http parser bindings • No memcpy() for http requests + hot code reloading!
  22. 22. Suitable Applications • Web frameworks • Real time • Crawlers
  23. 23. More Applications • Process monitoring • File uploading • Streaming
  24. 24. Demo Time!
  25. 25. Http Chat in 14 LoC var http = require('http'), messages = []; http.createServer(function(req, res) { res.sendHeader(200, {'Content-Type' : 'text/plain'}); if (req.url == '/') { res.sendBody(messages.join("n")); } else if (req.url !== '/favicon.ico') { messages.push(decodeURIComponent(req.url.substr(1))); res.sendBody('ok!'); } res.finish(); }).listen(4000);
  26. 26. The chat room: http://debuggable.com:4000/ Send a message: http://debuggable.com:4000/<msg>
  27. 27. Questions? ✎ @felixge $ http://debuggable.com/
  28. 28. Bonus Slides!
  29. 29. Dirty NoSQL for the little man!
  30. 30. Dirty JavaScript Views Memory Store Disk Persistence Speed > Safety Dirty
  31. 31. Dirty Hello World hello.js var Dirty = require('../lib/dirty').Dirty, posts = new Dirty('test.dirty'); posts.add({hello: 'dirty world!'}); posts.set('my-key', {looks: 'nice'}); $ node hello.js $ cat test.dirty {"hello":"dirty world!","_key":"3b8f86..."} {"looks":"nice","_key":"my-key"}
  32. 32. Reloading from Disk hello.js var Dirty = require('../lib/dirty').Dirty, posts = new Dirty('test.dirty'); posts.load() .addCallback(function() { p(posts.get('my-key')); }); $ node hello.js {"looks": "nice", "_key": "my-key"}
  33. 33. Filtering records hello.js var Dirty = require('../lib/dirty').Dirty, posts = new Dirty('test.dirty'); posts.load() .addCallback(function() { var docs = posts.filter(function(doc) { return ('hello' in doc); }); p(docs); }); $ node hello.js [{"hello": "dirty world!", "_key": "3b8f86..."}]
  34. 34. Benchmarks Do your own!
  35. 35. My Results • Set: 100k docs / sec • Iterate: 33 million docs / sec • Filter: 14 million docs / sec (on my laptop - your milage may vary)
  36. 36. Use Cases • Small projects (db < memory) • Rapid prototyping • Add HTTP/TCP interface and scale
  37. 37. http://github.com/felixge/node-dirty (or google for “dirty felixge”)

×