Your SlideShare is downloading. ×
0
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Hands On Node.js
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Hands On Node.js

3,503

Published on

Présentation donnée à la Marmite 2011

Présentation donnée à la Marmite 2011

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,503
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Node.jshttp://nodejs.org
  • 2. Summary● What ?● Why ?● Non blocking API example (file, dns...)● Event programming model● Socket.IO● Nodes modules (create module, express, coffeescript, underscore)● Unit Test
  • 3. 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
  • 4. What is Node ? http://codingrelic.geekhold.com/2010/08/nodejs-from-30000-feet.html
  • 5. 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, "127.0.0.1");console.log("Server running at http://127.0.0.1:1337/");~$ node server.js~$ curl http://127.0.0.1:1337/
  • 6. 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
  • 7. 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/
  • 8. Blocking API example print("hello"); sleep(2000); blocked!! print("world");
  • 9. Non blocking API example setTimeout(function() { console.log("world"); }, 2000); console.log("hello"); // No lock possible in Node // You cannot sleep !!!
  • 10. 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);
  • 11. Non blocking API examplevar data = File.read("file.txt", function(data) { parseResult(data);});myOtherCode();
  • 12. Event programming model● Events are the heart of Node.js● Everything is event based● You can create yours own events
  • 13. 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!);
  • 14. 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);}});});(...)};
  • 15. 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();
  • 16. Socket.IO Realtime apps Fast Every browser/deviceWeb Sockets, Long Pooling, Flash... Broadcasting Room Volatile / Acknowledgements
  • 17. ModulesCreate yours !hello.jsvar world = function() { alert("helloworld"); };exports.world = world;server.jsvar hello = require("./hello")hello.world();
  • 18. 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
  • 19. 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>
  • 20. Unit Tests with QUnit
  • 21. Limits- Cryptic error messagesnode.js:50 throw e; ^ Error: ECONNREFUSED, Connection refused at IOWatcher.callback (net:870:22) at node.js:607:9
  • 22. 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);});
  • 23. 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 ?
  • 24. 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
  • 25. Modules Node Boilerplate Express Event Emitter 2 Underscore Vows Coffeemate Node InspectorSpotify, Twitter, Gravatar, Dropbox, AWS,https://github.com/joyent/node/wiki/modules
  • 26. Node REPL~$ node> 1+2 3> [1, 2, 3].splice(1,2) [2, 3]> process.pid 2998> ...
  • 27. Questions ?

×