cocktail d’expérience informatiques             Genève 3 & 4 octobre 2011                   Seconde éditionAuteur    M. LE...
Node.jshttp://nodejs.org
Co-founder of Duchess Francehttp://www.java-freelance.fr@MathildeLemee                    Creator of Web Tambouille       ...
Summary•    What ? Why ?•    Non blocking API example•    Event programming model•    Express, Socket.IO and modules•    M...
What is Node ?                Server-side Javascript  Node.js javascript implementation is V8 Javascript              Engi...
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-Ty...
Why using Node ?•  Ryan Dahl: "Node.js project: To provide a purely evented,   non-blocking infrastructure to script highl...
Blocking API example  print("hello");  sleep(2000);      blocked!!  print("world");
Non blocking API examplevar data = File.read("file.txt");...// Here you have to wait... maybe a lot......// And your threa...
Non blocking API examplevar data = File.read("file.txt", function(data) {    parseResult(data);});// Here, your thread is ...
Event programming model•  Events are the heart of Node.js•  Everything is event based•  You can create yours own events
Event programming model              SampledummyEmitter.emit(myCustomEvent, myValue);dummyReceiver.on(myCustomEvent, funct...
Event programming model                   SampleSERVEURsocket.emit(news, { hello: world });socket.on(event, function (data...
NPM
Modules             Node Boilerplate             Event Emitter 2               Underscore                  Vows           ...
Express              Web Framework        Inspired by Sinatra (Ruby)Systèmes de templates (jade, Haml, EJS...)      var ap...
Expressapp.configure(development, function() {    server.set(views, __dirname + /views);    server.set(view engine, ejs); ...
Expressapp.get(/user/:id, function(req, res) {    res.send(user + req.params.id);});/users/:id?   /users/5   /users/user/:...
Socket.IO - Why ?WebSocket : Firefox 4, Chrome 4, Opera 10.70, and Safari 5.    Asynchronous communication from client to ...
Socket.IO - Howvar io = require(socket.io).listen(80);io.sockets.on(connection, function (socket) {   io.sockets.emit(info...
Socket.IO - How<script> var socket = io.connect(http://localhost); socket.on(connect, function () {    socket.on(info, fun...
Socket.IO - How•  custom messages :       socket.emit(my custom event,{data:xxx});•  volatile :      socket.volatile.emit(...
ModulesCreate yours !hello.jsvar world = function() {   alert("helloworld");};exports.world = world;server.jsvar hello = r...
Mini Hands OnGo Mathilde o/
Tests                                                          MLE•  Unit tests   o  Node.js provides its own assert modul...
Unit Tests with QUnit<script>$(document)    .ready(       function() {         module("1. Game");            test(        ...
Unit Tests with QUnit
Limits- Cryptic error messagesnode.js:50 throw e; ^ Error: ECONNREFUSED, Connection refused atIOWatcher.callback (net:870:...
Limits- Cryptic error messages- Only one thread (it is also an advantage)- Can be difficult to read- Non async lib/third p...
Node REPL~$ node> 1+2  3> [1, 2, 3].splice(1,2) [2, 3]> process.pid 2998> ...
LinksQuick tour : http://www.slideshare.net/the_undefined/nodejs-a-quick-tourNode + Websockets :http://www.slideshare.net/...
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
Upcoming SlideShare
Loading in …5
×

soft-shake.ch - Hands on Node.js

3,274 views

Published on

Mathilde Lemée & Romain Maton

La théorie, c’est bien, la pratique … aussi !

Venez nous rejoindre pour découvrir les profondeurs de Node.js !

Nous nous servirons d’un exemple pratique pour vous permettre d’avoir une premiere experience complete autour de Node.js et de vous permettre de vous forger un avis sur ce serveur Javascript qui fait parler de lui !


http://soft-shake.ch/2011/conference/sessions/incubator/2011/09/01/hands-on-nodejs.html

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

No Downloads
Views
Total views
3,274
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
35
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

soft-shake.ch - Hands on Node.js

  1. 1. cocktail d’expérience informatiques Genève 3 & 4 octobre 2011 Seconde éditionAuteur M. LEMEE & R. MATON Track IncubateurSession Node.js
  2. 2. Node.jshttp://nodejs.org
  3. 3. Co-founder of Duchess Francehttp://www.java-freelance.fr@MathildeLemee Creator of Web Tambouille http://www.web-tambouille.fr @rmat0n
  4. 4. Summary•  What ? Why ?•  Non blocking API example•  Event programming model•  Express, Socket.IO and modules•  Mini Hands On•  Unit Test•  Limits
  5. 5. What is Node ? Server-side Javascript Node.js javascript implementation is V8 Javascript Engine (Google Chrome)Provide an easy way to build scalable network programs Non blocking I/O Single Threaded Written by Ryah Dahl
  6. 6. What is Node ? http://codingrelic.geekhold.com/2010/08/nodejs-from-30000-feet.html
  7. 7. 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/
  8. 8. 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 (web, server, database...)
  9. 9. Blocking API example print("hello"); sleep(2000); blocked!! print("world");
  10. 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. 11. Non blocking API examplevar data = File.read("file.txt", function(data) { parseResult(data);});// Here, your thread is alive and continue working !!!myOtherCode();
  12. 12. Event programming model•  Events are the heart of Node.js•  Everything is event based•  You can create yours own events
  13. 13. Event programming model SampledummyEmitter.emit(myCustomEvent, myValue);dummyReceiver.on(myCustomEvent, function(data){ console.log(data);});
  14. 14. Event programming model SampleSERVEURsocket.emit(news, { hello: world });socket.on(event, function (data) { console.log(data);});CLIENTsocket.on(news, function (data) {console.log(data);socket.emit(event, { my: data });});
  15. 15. NPM
  16. 16. Modules Node Boilerplate Event Emitter 2 Underscore Vows Coffeemate Node InspectorSpotify, Twitter, Gravatar, Dropbox, AWS,https://github.com/joyent/node/wiki/modules
  17. 17. Express Web Framework Inspired by Sinatra (Ruby)Systèmes de templates (jade, Haml, EJS...) var app = express.createServer(); app.get(/, function(req, res) { res.send(Hello World); }); app.listen(3000);
  18. 18. Expressapp.configure(development, function() { server.set(views, __dirname + /views); server.set(view engine, ejs); app.use(express.static(__dirname + /public)); app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));});app.configure(production, function() { server.set(views, __dirname + /views); server.set(view engine, ejs); var aYear = 31557600000; app.use(express.static(__dirname + /public, { maxAge: aYear })); app.use(express.errorHandler());});
  19. 19. Expressapp.get(/user/:id, function(req, res) { res.send(user + req.params.id);});/users/:id? /users/5 /users/user/:id.:format? /user/12 /user/12.json/user/:id/:operation? /user/1 /user/1/edit/files/* /files/jquery.js /files/javascripts/jquery.js
  20. 20. Socket.IO - Why ?WebSocket : Firefox 4, Chrome 4, Opera 10.70, and Safari 5. Asynchronous communication from client to server. AJAX - XmlHttpRequest ? Flash / AJAX Long Polling Disconnection
  21. 21. Socket.IO - Howvar io = require(socket.io).listen(80);io.sockets.on(connection, function (socket) { io.sockets.emit(info, a player join the game); socket.on(chat, function (msg) { console.log(send a chat, msg); }); socket.on(disconnect, function () { sockets.emit(user disconnected); });});
  22. 22. Socket.IO - How<script> var socket = io.connect(http://localhost); socket.on(connect, function () { socket.on(info, function (data) { alert(data); }); }); socket.emit(chat,myMessage);</script>
  23. 23. Socket.IO - How•  custom messages : socket.emit(my custom event,{data:xxx});•  volatile : socket.volatile.emit(tweet,tweet);•  acknoledgements•  broadcast : socket.broadcast.emit(hello,world);•  room : socket.join(justin bieber fans)•  send a message in a room : io.sockets.in(my room).emit(hello,world);•  storing data : socket.set(name,Mathilde,function () { //callback });
  24. 24. ModulesCreate yours !hello.jsvar world = function() { alert("helloworld");};exports.world = world;server.jsvar hello = require("./hello")hello.world();
  25. 25. Mini Hands OnGo Mathilde o/
  26. 26. Tests MLE•  Unit tests o  Node.js provides its own assert module http://nodejs.org/docs/v0.5.6/api/assert.html o  QUnit (JQuery) http://docs.jquery.com/Qunit o  NodeUnit https://github.com/caolan/nodeunit o  Expresso https://github.com/visionmedia/expresso•  Integration tests o  Zombie.js http://zombie.labnotes.org•  Behavior Driven Development o  vowsjs http://vowsjs.org/ o  jasmine-node https://github.com/mhevery/jasmine-node•  https://github.com/joyent/node/wiki/modules#wiki-testing
  27. 27. 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>
  28. 28. Unit Tests with QUnit
  29. 29. Limits- Cryptic error messagesnode.js:50 throw e; ^ Error: ECONNREFUSED, Connection refused atIOWatcher.callback (net:870:22) at node.js:607:9client.on("error", function (err) { console.log("Error " + err);});
  30. 30. 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 ?
  31. 31. Node REPL~$ node> 1+2 3> [1, 2, 3].splice(1,2) [2, 3]> process.pid 2998> ...
  32. 32. 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

×