0% found this document useful (0 votes)
167 views35 pages

Sos11 1i4 Hands On Node Js 111205144521 Phpapp01 PDF

- The document discusses Node.js, a JavaScript runtime built on Chrome's V8 JavaScript engine. It provides an easy way to build scalable network programs using a non-blocking and event-driven architecture. - Key aspects of Node.js covered include the non-blocking I/O model, event-driven programming, modules like Express and Socket.IO for building web servers and real-time apps, and testing with tools like NodeUnit. - Potential limits discussed include cryptic error messages, the single-threaded nature, and lack of tooling compared to traditional servers. However, Node.js enables highly scalable and real-time applications using just JavaScript.

Uploaded by

arrebolando
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views35 pages

Sos11 1i4 Hands On Node Js 111205144521 Phpapp01 PDF

- The document discusses Node.js, a JavaScript runtime built on Chrome's V8 JavaScript engine. It provides an easy way to build scalable network programs using a non-blocking and event-driven architecture. - Key aspects of Node.js covered include the non-blocking I/O model, event-driven programming, modules like Express and Socket.IO for building web servers and real-time apps, and testing with tools like NodeUnit. - Potential limits discussed include cryptic error messages, the single-threaded nature, and lack of tooling compared to traditional servers. However, Node.js enables highly scalable and real-time applications using just JavaScript.

Uploaded by

arrebolando
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

cocktail dexprience informatiques

Genve 3 & 4 octobre 2011


Seconde dition
Track
Auteur
Session
Incubateur
M. LEMEE & R. MATON
Node.js
Node.js
http://nodejs.org






Co-founder of Duchess France
http://www.java-freelance.fr
@MathildeLemee




Creator of Web Tambouille
http://www.web-tambouille.fr
@rmat0n






Summary
What ? Why ?
Non blocking API example
Event programming model
Express, Socket.IO and modules
Mini Hands On
Unit Test
Limits
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
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 World\n");
}).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/
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...)
Blocking API example
print("hello");

sleep(2000);

print("world");
blocked!!
Non blocking API example
var 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 example
var data = File.read("file.txt", function(data) {
parseResult(data);
});

// Here, your thread is alive and continue working !!!
myOtherCode();
Event programming model



Events are the heart of Node.js


Everything is event based


You can create yours own events

Event programming model
Sample


dummyEmitter.emit('myCustomEvent', 'myValue');



dummyReceiver.on('myCustomEvent', function(data){
console.log(data);
});


Event programming model
Sample

socket.emit('news', { hello: 'world' });

socket.on('event', function (data) {
console.log(data);
});
SERVEUR
CLIENT
socket.on('news', function (data) {
console.log(data);
socket.emit('event', { my: 'data' });
});
NPM
Modules
Node Boilerplate

Event Emitter 2

Underscore

Vows

Coffeemate

Node Inspector

Spotify, Twitter, Gravatar, Dropbox, AWS,

https://github.com/joyent/node/wiki/modules
Express
var app = express.createServer();

app.get('/', function(req, res) {
res.send('Hello World');
});

app.listen(3000);
Web Framework

Inspired by Sinatra (Ruby)

Systmes de templates (jade, Haml, EJS...)
Express
app.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());
});
Express
app.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
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

Socket.IO - How
var 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');
});
});
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>

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 });



Modules
Create yours !

hello.js
var world = function() {
alert("helloworld");
};
exports.world = world;

server.js
var hello = require("./hello")
hello.world();
Mini Hands On
Go Mathilde \o/
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
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 prcdent");
});
...
</script>

Unit Tests with QUnit
Limits
- Cryptic error messages
node.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 ?

Node REPL
~$ node
> 1+2
3
> [1, 2, 3].splice(1,2)
[2, 3]
> process.pid
2998
> ...
Links
Quick tour : http://www.slideshare.net/the_undefined/nodejs-a-quick-tour

Node + Websockets :
http://www.slideshare.net/the_undefined/nodejs-a-quick-tour

Node beginner : http://nodebeginner.org/

The Node Beginner Book :
https://github.com/ManuelKiessling/NodeBeginnerBook

Hands on Node.js : http://nodetuts.com/handson-nodejs-book.html

Node.js in Action (t 2012): http://www.manning.com/cantelon

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy