• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Hands On Node.js

Hands On Node.js



Présentation donnée à la Marmite 2011

Présentation donnée à la Marmite 2011



Total Views
Views on SlideShare
Embed Views



1 Embed 22

http://paper.li 22



Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Hands On Node.js Hands On Node.js Presentation Transcript

    • Node.jshttp://nodejs.org
    • Summary● What ?● Why ?● Non blocking API example (file, dns...)● Event programming model● Socket.IO● Nodes modules (create module, express, coffeescript, underscore)● Unit Test
    • What is Node ?Provide an easy way to build scalable network programs Server-side Javascript Non blocking I/O Node.js javascript implementation is V8 Javascript Engine (Google Chrome) Written by Ryah Dahl
    • What is Node ? http://codingrelic.geekhold.com/2010/08/nodejs-from-30000-feet.html
    • What is Node ?var http = require(http);http.createServer(function (request, response) { res.writeHead(200, {"Content-Type": "text/plain"}); res.end("Hello Worldn");}).listen(1337, "");console.log("Server running at");~$ node server.js~$ curl
    • Why using Node ?● Ryan Dahl: "Node.js project: To provide a purely evented, non-blocking infrastructure to script highly concurrent programs" (http://yuilibrary.com/theater/ryan-dahl/dahl-node/)● Scalable software ● Non blocking I/O●● Same language and share code between server side and client side● JSON friendly
    • Modern Computer LantencyL1: 3 cyclesL2: 14 cycles Non blockingRAM: 250 cyclesDisk: 41 000 000 cycles BlockingNetwork: 240 000 000 cycles http://yuilibrary.com/theater/ryan-dahl/dahl-node/
    • Blocking API example print("hello"); sleep(2000); blocked!! print("world");
    • Non blocking API example setTimeout(function() { console.log("world"); }, 2000); console.log("hello"); // No lock possible in Node // You cannot sleep !!!
    • Non blocking API examplevar data = File.read("file.txt");...// Here you have to wait... maybe a lot......// And your thread is still alive... doing nothing......parseResult(data);
    • Non blocking API examplevar data = File.read("file.txt", function(data) { parseResult(data);});myOtherCode();
    • Event programming model● Events are the heart of Node.js● Everything is event based● You can create yours own events
    • Event programming model Samplevar events = require(events);var dummyObject = new events.EventEmitter();dummyObject.on(someOccurence, function(message){ console.log(message);});dummyObject.emit(someOccurence, Something happened!);
    • Event programming model module twittervar TwitterStream = function(opts) {this.username = opts.username;(...)};TwitterStream.prototype = new events.EventEmitter;module.exports = TwitterStream;TwitterStream.prototype.getTweets = function() {this.connection = http.request(opts, function(response) {(...)response.on(data, function(chunk) {try { (...)self.emit(tweet, JSON.parse(json));} catch(e) {self.emit(error, e);}});});(...)};
    • Event programming model application twittervar TwitterStream = require(./twitterstream);var twitter = new TwitterStream({username: username,(...)});twitter.on(tweet, function(tweet) {});twitter.on(error, function(e) {});twitter.getTweets();
    • Socket.IO Realtime apps Fast Every browser/deviceWeb Sockets, Long Pooling, Flash... Broadcasting Room Volatile / Acknowledgements
    • ModulesCreate yours !hello.jsvar world = function() { alert("helloworld"); };exports.world = world;server.jsvar hello = require("./hello")hello.world();
    • Tests● Unit tests ○ Node.js provides its own assert module http://nodejs.org/docs/v0.5.6 /api/assert.html ○ QUnit (JQuery) http://docs.jquery.com/Qunit ○ NodeUnit https://github.com/caolan/nodeunit ○ Expresso https://github.com/visionmedia/expresso● Integration tests ○ Zombie.js http://zombie.labnotes.org● Behavior Driven Development ○ vowsjs http://vowsjs.org/ ○ jasmine-node https://github.com/mhevery/jasmine-node● https://github.com/joyent/node/wiki/modules#wiki-testing
    • Unit Tests with QUnit<script>$(document) .ready( function() { module("1. Game"); test( "Connais la valeur du joueur suivant en fonction du joueur actuel", function() { game = new Game(); game.take(3); game.take(1); equals(game.otherPlayer(), "O", "O est le joueur précédent"); });...</script>
    • Unit Tests with QUnit
    • Limits- Cryptic error messagesnode.js:50 throw e; ^ Error: ECONNREFUSED, Connection refused at IOWatcher.callback (net:870:22) at node.js:607:9
    • Limits- Cryptic error messagesnode.js:50 throw e; ^ Error: ECONNREFUSED, Connection refused at IOWatcher.callback (net:870:22) at node.js:607:9 => client.on("error", function (err) { console.log("Error " + err);});
    • Limits- Cryptic error messages- Only one thread (it is also an advantage)- Can be difficult to read- Non async lib/third party decrease perfs- IDE, Tooling, Debugger, Profiler- How to choose a good module ?
    • LinksQuick tour : http://www.slideshare.net/the_undefined/nodejs-a-quick-tourNode + Websockets : http://www.slideshare.net/the_undefined/nodejs-a-quick-tourNode beginner : http://nodebeginner.org/The Node Beginner Book : https://github.com/ManuelKiessling/NodeBeginnerBookHands on Node.js : http://nodetuts.com/handson-nodejs-book.htmlNode.js in Action (été 2012): http://www.manning.com/cantelon
    • Modules Node Boilerplate Express Event Emitter 2 Underscore Vows Coffeemate Node InspectorSpotify, Twitter, Gravatar, Dropbox, AWS,https://github.com/joyent/node/wiki/modules
    • Node REPL~$ node> 1+2 3> [1, 2, 3].splice(1,2) [2, 3]> process.pid 2998> ...
    • Questions ?