Node.js: Difference between revisions

From air
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
==Description==
==Description==


[http://nodejs.org/ Node.js] est un canevas événementiel pour réaliser des applications serveur en [[Javascript]].
[http://nodejs.org/ Node.js] est un canevas événementiel pour réaliser des applications serveur en [[Javascript]]. L'ensemble des événements est traité par une seule thread: Node.js privilégie les appels non bloquants. Node.js facilite le push (serveur --> client Web)


Cours
* Formation Valtech : http://public.valtech-training.fr/DekNOD/slideshow/dist/#1


===Lancement===
===Lancement===
Line 24: Line 26:
node httpserver.js 8080
node httpserver.js 8080
</pre>
</pre>



===API===
===API===
Line 64: Line 67:
===Canevas===
===Canevas===
Des nombreux canevas sont construits sur Node.js
Des nombreux canevas sont construits sur Node.js
* ql.io
* ...


* [https://github.com/voodootikigod/node-serialport node-serialport] access serial ports for reading and writing
* [https://github.com/ecto/duino duino] A framework for working with Arduinos in node.js
* [https://github.com/jgautier/firmata firmata] A Node library to interact with an Arduino running the firmata protocol.


===Registre===
===Registre===
Plusieurs sont catalogués depuis le registre https://npmjs.org/
Plusieurs modules sont catalogués depuis le registre https://npmjs.org/
et installables avec npm (Node Package Manager)
et installables avec npm (Node Package Manager)


Line 85: Line 87:
# Installer un package pour le contrôle d'une carte Arduino
# Installer un package pour le contrôle d'une carte Arduino
npm install duino
npm install duino

# Installer un package
npm publish mypackage
</pre>
</pre>

===Modules pour les projets AIR===
* [https://github.com/voodootikigod/node-serialport node-serialport] access serial ports for reading and writing
* [https://github.com/ecto/duino duino] A framework for working with Arduinos in node.js
* [https://github.com/jgautier/firmata firmata] A Node library to interact with an Arduino running the firmata protocol.


==Exemples==
==Exemples==

===Lancement===
Aide
<pre>
node --help
</pre>

Interactif
<pre>
node
</pre>

En ligne
<pre>
node -p -e "Boolean(process.stdout.isTTY)"
</pre>

En argument (process.argv[1])
<pre>
node httpserver.js 8080
</pre>



===HTTP script===
===HTTP script===

Revision as of 08:59, 3 March 2013

Description

Node.js est un canevas événementiel pour réaliser des applications serveur en Javascript. L'ensemble des événements est traité par une seule thread: Node.js privilégie les appels non bloquants. Node.js facilite le push (serveur --> client Web)

Cours

Lancement

Aide

node --help

Interactif

node

En ligne

node -p -e "Boolean(process.stdout.isTTY)"

En argument (process.argv[1])

node httpserver.js 8080


API

Des nombreuses bibliothèques sont disponibles : Assertion Testing, Buffer, C/C++ Addons, Child Processes, Cluster, Crypto, Debugger, DNS, Domain, Events, File System, Globals, HTTP, HTTPS, Modules, Net, OS, Path, Process, Punycode, Query Strings, Readline, REPL, STDIO, Stream, String Decoder, Timers, TLS/SSL, TTY, UDP/Datagram, URL, Utilities, VM, ZLIB

Canevas

Des nombreux canevas sont construits sur Node.js

  • ql.io
  • ...


Registre

Plusieurs modules sont catalogués depuis le registre https://npmjs.org/ et installables avec npm (Node Package Manager)

# Voir les packages disponibles 
npm ls

# Voir les packages installés
npm ls installed

# Rechercher un package
npm search duino

# Installer un package pour le contrôle d'une carte Arduino
npm install duino

# Installer un package
npm publish mypackage

Modules pour les projets AIR

  • node-serialport access serial ports for reading and writing
  • duino A framework for working with Arduinos in node.js
  • firmata A Node library to interact with an Arduino running the firmata protocol.

Exemples

Lancement

Aide

node --help

Interactif

node

En ligne

node -p -e "Boolean(process.stdout.isTTY)"

En argument (process.argv[1])

node httpserver.js 8080


HTTP script

var port = process.argv[2];

var http = require("http");

function onRequest(request, response) {
  console.log("Receiving a request");
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}

http.createServer(onRequest).listen(port);
console.log("Server started. ^C to kill it");
node hello.js 8888


Express

var express = require('express');
var app = express();
app.get('/hello.txt', function(req, res){
  res.send('Hello World');
});
app.listen(3000);


node install express
node express.js 3000
start http://localhost:3000/hello.txt

UDP Datagrams

var dgram = require("dgram");

var server = dgram.createSocket("udp4");

server.on("message", function (msg, rinfo) {
  console.log("server got: " + msg + " from " +
    rinfo.address + ":" + rinfo.port);
});

server.on("listening", function () {
  var address = server.address();
  console.log("server listening " +
      address.address + ":" + address.port);
});

server.bind(41234);
// server listening 0.0.0.0:41234

node dgram.js


Crypto: SHA1 Hashing


var filename = process.argv[2];
var crypto = require('crypto');
var fs = require('fs');

var shasum = crypto.createHash('sha1');

var s = fs.ReadStream(filename);
s.on('data', function(d) {
  shasum.update(d);
});

s.on('end', function() {
  var d = shasum.digest('hex');
  console.log(d + '  ' + filename);
});


node hash.js hash.js


TTY

process.stdout.on('resize', function() {
  console.log('screen size has changed!');
  console.log(process.stdout.columns + 'x' + process.stdout.rows);
});


node resize.js


$ node -p -e "Boolean(process.stdout.isTTY)"
true
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
false


Cluster

var port = process.argv[2];
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {
  // Workers can share any TCP connection
  // In this case its a HTTP server
  http.createServer(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(port);
}


node httpcluster.js 8080