node.js - javascript ovunqueRoma, 26 ottobre 2013
LinuxDay Roma 2013
Valerio Coltrè → . ←Claudio Mignanti .
Cosa è
node.js è una piattaforma basata su V8, motore di google che interpreta javascript:● facile approccio per gli sviluppatori HTML5● poche risorse richieste● programmazione single thread● webserver scriptabile in js● perfetto per applicazioni
data-intensive
1. http://nodejs.org/2. http://mashable.com/2011/03/10/node-js/ http://goo.gl/4VuKS6
V8, l’interprete
V8 è l'engine javascript originalmentesviluppato per Chrome.
1. http://code.google.com/p/v8/
● Molto veloce, profiling avanzato.● Completamente opensource.
http://goo.gl/4VuKS6
Dove si usa
Ambiti d'uso:● REST API (JSON)● NodeCopter● Coder for Raspberry PI● Web Developer Tools (Grunt, Bower)
Altri punti a favore:● community molto aperta e fervente● performance cpu-bound & data-bound.● nuove tecnologie (websocket)
http://goo.gl/4VuKS6
javascript
Caratteristiche:● linguaggio che supporta l'OOP● interpretato a runtime● debolmente tipato
http://goo.gl/4VuKS6
Assi nella manica:● closure● prototipizzazione● regex native (senza librerie)● funzioni anonime native● simple to learn, hard to master
event loop
Gli interpreti javascript sono single thread● basato su eventi
○ funzioni in listening sull'evento● stack di chiamate● accesso asincrono alle risorse
http://goo.gl/4VuKS6
Specificatamente per nodejs:● possibilità di accesso sincrono al filesystem● buffer● multiprocesso● ...ed altre nodejs API
npm - node package manager
http://goo.gl/4VuKS6
nodejs è basato su moduli● AMD-compliant● ogni modulo é isolato (fn wrap)
Per gestire le (complesse) dipendenze c'è NPM
moduli famosi:● express Sinatra inspired web development framework
● async Higher-order functions and common patterns for async. code
● grunt The JavaScript Task Runner
● phone-gap allows you to create mobile apps using standardized web APIs
package.json
Gestisce le dipendenze con altri moduli nodejs
● Scaffold veloce con npm init● non re-inventare la ruota● moduli utili anche < 100 righe● one line install
http://goo.gl/4VuKS6
http://package.json.nodejitsu.com/
package.json
http://goo.gl/4VuKS6
Cosa si puo’ fare con nodejs:
Esempio pratico - tail -f
http://goo.gl/4VuKS6
tail.js → chunked.js● comportamento analogo a tail -f● versione web● chunked response (HTTP 1.1)
http://goo.gl/6Ozcc9
API di nodejs utilizzate:● http - web server● fs - lettura filesystem● stream in lettura/scrittura
Esempio pratico - tail -f
http://goo.gl/4VuKS6
http://nodejs.org/api/
http://goo.gl/6Ozcc9
conclusioni
? ← https://github.com/claudyus https://github.com/mrgamer → https://github.com/TopCS
http://goo.gl/4VuKS6 →