2014.04.04 Sviluppare applicazioni web (completamente) on line con Visual Stu...
node.js everywhere
1. node.js - javascript ovunque
Roma, 26 ottobre 2013
LinuxDay Roma 2013
Valerio Coltrè → .
←Claudio Mignanti .
2. 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
3. V8, l’interprete
V8 è l'engine javascript originalmente
sviluppato per Chrome.
1. http://code.google.com/p/v8/
● Molto veloce, profiling avanzato.
● Completamente opensource.
http://goo.gl/4VuKS6
4. 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
5. 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
6. 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
7. 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
8. 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/
10. 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
11. 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