Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Node.js     http://nodejs.org
<ul><li>Co-founder of Duchess France </li></ul><ul><li>http://www.java-freelance.fr </li></ul><ul><li>@MathildeLemee </li>...
Summary <ul><ul><li>What  ? Why  ? </li></ul></ul><ul><ul><li>Non blocking API example </li></ul></ul><ul><ul><li>Event pr...
What is Node ? <ul><li>Server-side Javascript </li></ul><ul><li>Node.js javascript implementation is V8 Javascript Engine ...
What is Node ? http://codingrelic.geekhold.com/2010/08/nodejs-from-30000-feet.html
What is Node ? <ul><li>var http = require( 'http' ); </li></ul><ul><li>http.createServer(function (request, response) {  <...
Why using Node ? <ul><ul><li>Ryan Dahl: &quot;Node.js project: To provide a purely evented, non-blocking infrastructure to...
Blocking API example <ul><li>print( &quot;hello&quot; ); </li></ul><ul><li>sleep( 2000 ); </li></ul><ul><li>print( &quot;w...
Non blocking API example <ul><li>var data = File.read( &quot;file.txt&quot; ); </li></ul><ul><li>... </li></ul><ul><li>// ...
Non blocking API example <ul><li>var data = File.read( &quot;file.txt&quot; , function(data) { </li></ul><ul><li>     pars...
 
Event programming model <ul><ul><li>Events are the heart of Node.js </li></ul></ul><ul><ul><li>Everything is event based <...
Event programming model Sample <ul><li>dummyEmitter.emit('myCustomEvent', 'myValue'); </li></ul><ul><li>dummyReceiver.on('...
Event programming model Sample <ul><li>socket.emit('news', { hello: 'world' }); </li></ul><ul><li>socket.on('event', funct...
NPM
 
Modules <ul><li>Node Boilerplate </li></ul><ul><li>Event Emitter 2 </li></ul><ul><li>Underscore </li></ul><ul><li>Vows </l...
Express var app = express.createServer(); app.get( '/' , function(req, res) {      res.send( 'Hello World' ); }); app.list...
Express <ul><li>app.configure( 'development' , function() { </li></ul><ul><li>     server.set( 'views' ,  __dirname  +  '/...
Express <ul><li>app.get( '/user/:id' , function(req, res) { </li></ul><ul><li>     res.send( 'user'  + req.params.id); </l...
Socket.IO - Why ? <ul><li>WebSocket :   Firefox 4, Chrome 4, Opera 10.70, and Safari 5. </li></ul><ul><li>Asynchronous com...
Socket.IO - How <ul><li>var  io  =  require( 'socket.io' ).listen( 80 ); </li></ul><ul><li>io.sockets.on( 'connection' ,  ...
Socket.IO - How <ul><li><script>  </li></ul><ul><li>    var  socket  =  io.connect( 'http://localhost' ); </li></ul><ul><l...
Socket.IO - How <ul><ul><li>custom messages :      socket.emit('my custom event',{data:xxx}); </li></ul></ul><ul><ul><li>v...
Modules <ul><li>Create yours ! </li></ul><ul><li>hello.js </li></ul><ul><li>var world = function() { </li></ul><ul><li>   ...
Mini Hands On <ul><li>Go Mathilde o/ </li></ul>
Tests <ul><li>     MLE </li></ul><ul><ul><li>Unit tests </li></ul></ul><ul><ul><ul><li>Node.js provides its own assert mod...
Unit Tests with QUnit <ul><li><script> </li></ul><ul><li>$(document) </li></ul><ul><li>     .ready( </li></ul><ul><li>    ...
Unit Tests with QUnit
Limits <ul><li>- Cryptic error messages </li></ul><ul><li>node.js:50 throw e; ^  </li></ul><ul><li>  Error: ECONNREFUSED, ...
Limits <ul><li>- Cryptic error messages </li></ul><ul><li>  </li></ul><ul><li>- Only one thread (it is also an advantage) ...
Node REPL <ul><li>~$ node </li></ul><ul><li>> 1+2 </li></ul><ul><li>    3 </li></ul><ul><li>> [1, 2, 3].splice(1,2) </li><...
Links <ul><li>Quick tour :  http://www.slideshare.net/the_undefined/nodejs-a-quick-tour </li></ul><ul><li>Node + Websocket...
Prochain SlideShare
Chargement dans…5
×

Présentation de Node.js

6 108 vues

Publié le

Présentation donnée aux conférences SoftShake et RivieraDev 2011.

Publié dans : Technologie
  • Soyez le premier à commenter

Présentation de Node.js

  1. 1. Node.js     http://nodejs.org
  2. 2. <ul><li>Co-founder of Duchess France </li></ul><ul><li>http://www.java-freelance.fr </li></ul><ul><li>@MathildeLemee </li></ul><ul><li>Creator of Web Tambouille </li></ul><ul><li>http://www.web-tambouille.fr </li></ul><ul><li>@rmat0n </li></ul>
  3. 3. Summary <ul><ul><li>What  ? Why  ? </li></ul></ul><ul><ul><li>Non blocking API example </li></ul></ul><ul><ul><li>Event programming model </li></ul></ul><ul><ul><li>Express, Socket.IO and modules </li></ul></ul><ul><ul><li>Mini Hands On </li></ul></ul><ul><ul><li>Unit Test </li></ul></ul><ul><ul><li>Limits </li></ul></ul>
  4. 4. What is Node ? <ul><li>Server-side Javascript </li></ul><ul><li>Node.js javascript implementation is V8 Javascript Engine (Google Chrome) </li></ul><ul><li>Provide an easy way to build scalable network programs </li></ul><ul><li>Non blocking I/O </li></ul><ul><li>Single Threaded </li></ul><ul><li>Written by Ryah Dahl </li></ul>
  5. 5. What is Node ? http://codingrelic.geekhold.com/2010/08/nodejs-from-30000-feet.html
  6. 6. What is Node ? <ul><li>var http = require( 'http' ); </li></ul><ul><li>http.createServer(function (request, response) {  </li></ul><ul><li>     res.writeHead( 200 , { &quot;Content-Type&quot; : &quot;text/plain&quot; }); </li></ul><ul><li>     res.end( &quot;Hello Worldn&quot; ); </li></ul><ul><li>}).listen( 1337 , &quot;127.0.0.1&quot; ); </li></ul><ul><li>console.log( &quot;Server running at http://127.0.0.1:1337/&quot; ); </li></ul><ul><li>~$ node server.js </li></ul><ul><li>~$ curl http://127.0.0.1:1337/ </li></ul>
  7. 7. Why using Node ? <ul><ul><li>Ryan Dahl: &quot;Node.js project: To provide a purely evented, non-blocking infrastructure to script highly concurrent programs&quot; ( http://yuilibrary.com/theater/ryan-dahl/dahl-node/ ) </li></ul></ul><ul><ul><li>Scalable software </li></ul></ul><ul><ul><li>Non blocking I/O </li></ul></ul><ul><ul><li>Same language and share code between server side and client side </li></ul></ul><ul><ul><li>JSON friendly (web, server, database...) </li></ul></ul>
  8. 8. Blocking API example <ul><li>print( &quot;hello&quot; ); </li></ul><ul><li>sleep( 2000 ); </li></ul><ul><li>print( &quot;world&quot; ); </li></ul>blocked!!
  9. 9. Non blocking API example <ul><li>var data = File.read( &quot;file.txt&quot; ); </li></ul><ul><li>... </li></ul><ul><li>// Here you have to wait... maybe a lot... </li></ul><ul><li>... </li></ul><ul><li>// And your thread is still alive... doing nothing... </li></ul><ul><li>... </li></ul><ul><li>parseResult(data); </li></ul>
  10. 10. Non blocking API example <ul><li>var data = File.read( &quot;file.txt&quot; , function(data) { </li></ul><ul><li>    parseResult(data); </li></ul><ul><li>}); </li></ul><ul><li>// Here, your thread is alive and continue working !!! </li></ul><ul><li>myOtherCode(); </li></ul>
  11. 12. Event programming model <ul><ul><li>Events are the heart of Node.js </li></ul></ul><ul><ul><li>Everything is event based </li></ul></ul><ul><ul><li>You can create yours own events </li></ul></ul>
  12. 13. Event programming model Sample <ul><li>dummyEmitter.emit('myCustomEvent', 'myValue'); </li></ul><ul><li>dummyReceiver.on('myCustomEvent', function(data){ </li></ul><ul><li>    console.log(data); </li></ul><ul><li>}); </li></ul>
  13. 14. Event programming model Sample <ul><li>socket.emit('news', { hello: 'world' }); </li></ul><ul><li>socket.on('event', function (data) { </li></ul><ul><li>     console.log(data); </li></ul><ul><li>}); </li></ul>SERVEUR CLIENT socket.on('news', function (data) { console.log(data); socket.emit('event', { my: 'data' }); });
  14. 15. NPM
  15. 17. Modules <ul><li>Node Boilerplate </li></ul><ul><li>Event Emitter 2 </li></ul><ul><li>Underscore </li></ul><ul><li>Vows </li></ul><ul><li>Coffeemate </li></ul><ul><li>Node Inspector </li></ul><ul><li>Spotify, Twitter, Gravatar, Dropbox, AWS,  </li></ul><ul><li>https://github.com/joyent/node/wiki/modules </li></ul>
  16. 18. Express var app = express.createServer(); app.get( '/' , function(req, res) {      res.send( 'Hello World' ); }); app.listen( 3000 ); Web Framework Inspired by Sinatra (Ruby) Systèmes de templates (jade, Haml, EJS...)
  17. 19. Express <ul><li>app.configure( 'development' , function() { </li></ul><ul><li>     server.set( 'views' , __dirname + '/views' ); </li></ul><ul><li>     server.set( 'view engine' , 'ejs' ); </li></ul><ul><li>     app.use(express.static( __dirname + '/public' )); </li></ul><ul><li>     app.use(express.errorHandler({ dumpExceptions: true , </li></ul><ul><li>                                                         showStack: true })); </li></ul><ul><li>}); </li></ul><ul><li>app.configure( 'production' , function() { </li></ul><ul><li>     server.set( 'views' , __dirname +  '/views' ); </li></ul><ul><li>     server.set( 'view engine' ,  'ejs' ); </li></ul><ul><li>     var aYear = 31557600000 ; </li></ul><ul><li>     app.use(express.static( __dirname + '/public' , { 'maxAge' : aYear })); </li></ul><ul><li>     app.use(express.errorHandler()); </li></ul><ul><li>}); </li></ul>
  18. 20. Express <ul><li>app.get( '/user/:id' , function(req, res) { </li></ul><ul><li>     res.send( 'user' + req.params.id); </li></ul><ul><li>}); </li></ul><ul><li>'/users/:id?' </li></ul><ul><li>     /users/5  </li></ul><ul><li>     /users </li></ul><ul><li>'/user/:id.:format?' </li></ul><ul><li>     /user/12 </li></ul><ul><li>    /user/12.json </li></ul><ul><li>'/user/:id/:operation?' </li></ul><ul><li>     /user/1 </li></ul><ul><li>     /user/1/edit </li></ul><ul><li>'/files/*' </li></ul><ul><li>     /files/jquery.js </li></ul><ul><li>     /files/javascripts/jquery.js </li></ul>
  19. 21. Socket.IO - Why ? <ul><li>WebSocket :   Firefox 4, Chrome 4, Opera 10.70, and Safari 5. </li></ul><ul><li>Asynchronous communication from client to server. </li></ul><ul><li>AJAX - XmlHttpRequest ? </li></ul><ul><li>  </li></ul><ul><li>Flash / AJAX Long Polling </li></ul><ul><li>Disconnection </li></ul>
  20. 22. Socket.IO - How <ul><li>var io = require( 'socket.io' ).listen( 80 ); </li></ul><ul><li>io.sockets.on( 'connection' , function (socket) { </li></ul><ul><li>     io.sockets.emit( 'info' , 'a player join the game'); </li></ul><ul><li>     socket.on( 'chat' , function (msg) { </li></ul><ul><li>         console.log( 'send a chat' , msg); </li></ul><ul><li>     }); </li></ul><ul><li>     socket.on( 'disconnect' , function () { </li></ul><ul><li>         sockets.emit( 'user disconnected' ); </li></ul><ul><li>     }); </li></ul><ul><li>}); </li></ul>
  21. 23. Socket.IO - How <ul><li><script>  </li></ul><ul><li>    var socket = io.connect( 'http://localhost' ); </li></ul><ul><li>    socket.on( 'connect' , function () { </li></ul><ul><li>      socket.on( 'info' , function (data) {  </li></ul><ul><li>          alert(data); </li></ul><ul><li>      }); </li></ul><ul><li>    }); </li></ul><ul><li>    socket.emit( 'chat' ,myMessage); </li></ul><ul><li></script> </li></ul>
  22. 24. Socket.IO - How <ul><ul><li>custom messages :      socket.emit('my custom event',{data:xxx}); </li></ul></ul><ul><ul><li>volatile :     socket.volatile.emit('tweet',tweet); </li></ul></ul><ul><ul><li>acknoledgements </li></ul></ul><ul><ul><li>broadcast :     socket.broadcast.emit('hello','world'); </li></ul></ul><ul><ul><li>room :     socket.join('justin bieber fans') </li></ul></ul><ul><ul><li>send a message in a room :     io.sockets.in('my room').emit('hello','world'); </li></ul></ul><ul><ul><li>storing data :     socket.set('name','Mathilde',function () { //callback }); </li></ul></ul>
  23. 25. Modules <ul><li>Create yours ! </li></ul><ul><li>hello.js </li></ul><ul><li>var world = function() { </li></ul><ul><li>     alert( &quot;helloworld&quot; ); </li></ul><ul><li>}; </li></ul><ul><li>exports.world = world; </li></ul><ul><li>server.js </li></ul><ul><li>var hello = require( &quot;./hello&quot; ) </li></ul><ul><li>hello.world(); </li></ul>
  24. 26. Mini Hands On <ul><li>Go Mathilde o/ </li></ul>
  25. 27. Tests <ul><li>    MLE </li></ul><ul><ul><li>Unit tests </li></ul></ul><ul><ul><ul><li>Node.js provides its own assert module  http://nodejs.org/docs/v0.5.6/api/assert.html </li></ul></ul></ul><ul><ul><ul><li>QUnit (JQuery)  http://docs.jquery.com/Qunit </li></ul></ul></ul><ul><ul><ul><li>NodeUnit  https://github.com/caolan/nodeunit </li></ul></ul></ul><ul><ul><ul><li>Expresso  https://github.com/visionmedia/expresso </li></ul></ul></ul><ul><ul><li>Integration tests </li></ul></ul><ul><ul><ul><li>Zombie.js  http://zombie.labnotes.org </li></ul></ul></ul><ul><ul><li>  Behavior Driven Development </li></ul></ul><ul><ul><ul><li>vowsjs  http://vowsjs.org/ </li></ul></ul></ul><ul><ul><ul><li>jasmine-node  https://github.com/mhevery/jasmine-node </li></ul></ul></ul><ul><ul><li>https://github.com/joyent/node/wiki/modules#wiki-testing </li></ul></ul>
  26. 28. Unit Tests with QUnit <ul><li><script> </li></ul><ul><li>$(document) </li></ul><ul><li>     .ready( </li></ul><ul><li>         function() { </li></ul><ul><li>             module(&quot;1. Game&quot;); </li></ul><ul><li>                 test( </li></ul><ul><li>                     &quot;Connais la valeur du joueur suivant en fonction du joueur actuel&quot;, </li></ul><ul><li>                     function() { </li></ul><ul><li>                             game = new Game(); </li></ul><ul><li>                             game.take(3); </li></ul><ul><li>                             game.take(1); </li></ul><ul><li>                             equals(game.otherPlayer(), &quot;O&quot;, &quot;O est le joueur précédent&quot;); </li></ul><ul><li>                 }); </li></ul><ul><li>... </li></ul><ul><li></script> </li></ul><ul><li>                 </li></ul>
  27. 29. Unit Tests with QUnit
  28. 30. Limits <ul><li>- Cryptic error messages </li></ul><ul><li>node.js:50 throw e; ^  </li></ul><ul><li>  Error: ECONNREFUSED, Connection refused at IOWatcher.callback (net:870:22) at node.js:607:9 </li></ul><ul><li>  </li></ul>client.on(&quot;error&quot;, function (err) {      console.log(&quot;Error &quot; + err); });   
  29. 31. Limits <ul><li>- Cryptic error messages </li></ul><ul><li>  </li></ul><ul><li>- Only one thread (it is also an advantage) </li></ul><ul><li>- Can be difficult to read </li></ul><ul><li>- Non async lib/third party decrease perfs </li></ul><ul><li>- IDE, Tooling, Debugger, Profiler </li></ul><ul><li>- How to choose a good module ? </li></ul>
  30. 32. Node REPL <ul><li>~$ node </li></ul><ul><li>> 1+2 </li></ul><ul><li>    3 </li></ul><ul><li>> [1, 2, 3].splice(1,2) </li></ul><ul><li>  [2, 3] </li></ul><ul><li>> process.pid </li></ul><ul><li>  2998 </li></ul><ul><li>> ... </li></ul>
  31. 33. Links <ul><li>Quick tour :  http://www.slideshare.net/the_undefined/nodejs-a-quick-tour </li></ul><ul><li>Node + Websockets :  http://www.slideshare.net/the_undefined/nodejs-a-quick-tour </li></ul><ul><li>Node beginner :  http://nodebeginner.org/ </li></ul><ul><li>The Node Beginner Book :  https://github.com/ManuelKiessling/NodeBeginnerBook </li></ul><ul><li>Hands on Node.js :  http://nodetuts.com/handson-nodejs-book.html </li></ul><ul><li>Node.js in Action (été 2012):  http://www.manning.com/cantelon </li></ul>

×