Node.js: Difference between revisions

From air
Jump to navigation Jump to search
Line 64: Line 64:
===Canevas===
===Canevas===
Des nombreux canevas sont construits sur Node.js
Des nombreux canevas sont construits sur Node.js

* [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==

Revision as of 20:32, 2 March 2013

Description

Node.js est un canevas événementiel pour réaliser des applications serveur en Javascript.


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

  • 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

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

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