Célia CACCIATORE

Jérémy BOSSUT

Jonathan GEOFFROY

15 Décembre 2014

  • Orienté prototype
  • Faiblement typé
  • Fonctionnel

Prototype

var myObject = {};
myObject.sayHello(); // Error: undefined 
myObject.sayHello = function () {
    console.log("Hello World!");
}
myObject.sayHello(); // "Hello World!"

Faiblement typé

var intThree = 3;
var stringThree = "3";

intThree == stringThree; // true
intThree === stringThree; // false

Fonctionnel

var myParameterFunction = function() {
  // Code to execute
}
myFunction(paramFunction) {
	paramFunction();
}
// function given as a parameter of another
myFunction(myParameterFunction);

Portée des variables

var animal = "I am a dog";

var transformToCat = function () {
	animal = "I am a cat";
}
transformToCat();
console.log(animal); // "I am a cat"

Portée des variables

var animal = "I am a dog";

var transformToCat = function () {
    var animal = "Into function, I am a cat";
    console.log(animal) // "Into function, I am a cat"
}
transformToCat();
console.log(animal); // "I am a dog"

Coût I/O

Cache L1

Cache L2

RAM

Disque

Réseau

3 cycles

14 cycles

250 cycles

41 000 000 cycles

240 000 000 cycles

  • Non bloquant
  • Mono-threadé
  • Basé sur les évènements

Bloquant VS Non bloquant

Gain de temps

Resynchroniser

Évènements

Callback

var processing = function (callback) {
    // Processing
    callback(result);
}
processing(function (res) {
  console.log('processing ended!');
})

Fonction appelée en fin de traitement

Callback Hell

fs.readdir(source, function(err, files) {
  files.forEach(function(filename, fileIndex) {
    gm(source + filename).size(function(err, values) {
      widths.forEach(function(width, widthIndex) {
        this.resize(width, height).write(filename, function(err) {
          if (err) console.log('Error writing file: ' + err)
        });
      }.bind(this))
    });
  });
});

Programmation événementielle

var res = myFunction() {
  // Processing
});
res.on('data', function (chunk) {});
res.on('error', function(e) {});
res.on('end', function(e) {});

Appel à un module

Utilisation du module

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');

Déclaration d'un module 

Utilisation du module 

// fichier myModule.js
myPrivateFunction : function() { ... }

module.exports {
    myFunction : function() {
        myPrivateFunction();
    }
}
// fichier myApp.js
var myModule = require('./myModule.js');

myModule.myFunction();

112k+ modules

$ npm install [-g] nomModule
$ npm install

async

var async = require('async');
// Process each item of array
async.each(array, function process(itemOfArray, asyncCallback) {
    console.log('process: ' + itemOfArray);
    asyncCallback();
  });
  },
  // Callback called as soon as all files are processed
  function () {
  	console.log('All files processed');
});

Faciliter le développement asynchrone

fs: File System

var fs = require('fs');
fs.readFile(file, function parse(err, data) {
	console.log('file content: ' + data);
});

Disque dur :

  • mkdir

  • chmod

  • readFile
  • createReadStream

  • createWriteStream

underscore: Collection & Parcours

Itération

var _ = require('underscore');
_.each(aMap, function (value, key) {
    console.log(key + ' : ' + value);
}

Map

_.map([1, 2, 3], 
    function(num) { return num * 3 ; });
// [3, 6, 9]

Filtre

var evens = _.filter([1, 2, 3, 4, 5, 6], 
    function(num) { return num % 2 == 0; });
// [2, 4, 6]

http: Serveur

Écoute serveur :

  • url

  • port

  • mot (GET, POST, ...)
var http = require('http');
http.createServer(function (req, res) {
  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/');

express

Forces

  • Flux
  • Scalable
  • NPM

Faiblesses

  • Temps CPU
  • Jeune
  • Complexe

Architecture

Node

By Jérémy BOSSUT