Upload
devday
View
188
Download
2
Embed Size (px)
Citation preview
The Sexy Platform
Michele NastiNapoli Dev Day - Rework - 24/02/2016
Chi sono?
● http://michelenasti.com
● Twitter (@micnasti)
● ... e tutti gli altri servizi on line
(Stack Overflow, Github, ....)
What is NodeJS ?
- Node NON E’ un webserver
- … è un modo come un altro per eseguire codice sulla
propria macchina
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
Installazione e primo avvio
- installer per Win, Mac & Linux su nodejs.org
- node console (REPL):
$ node
> console.log('Hello World');
Hello World
undefined
- Oppure creare un file che contiene
- console.log('Hello World')
- ed eseguirlo con node file.js
Proviamo a fare le cose serie
- Supponiamo di avere un file di log di questo tipo:
2013-08-09T13:50:33.166Z A 2
2013-08-09T13:51:33.166Z B 1
2013-08-09T13:52:33.166Z C 6
2013-08-09T13:53:33.166Z B 8
2013-08-09T13:54:33.166Z B 5
- Scriviamo un parser per SOMMARE i numeri per ogni lettera- Output atteso:
{ A : 2, B: 14, C: 6}
“vuttamm e’ mman:”un Parser in JS
Callback Asincrone
- è il meccanismo preferito in NodeJS : “quando hai completato l’
operazione, esegui questo codice”
- NodeJS è Single-threaded
- la verità: “tutto ciò che scrivete voi è single threaded”
- “il 99% del tempo di una webapp è speso aspettando dati dal DB”
- “Callback Hell” ⇒
Organizzazione del Codice
- Come organizzare il codice in più file (classi…
moduli… ) ?
- Node is not Ruby on Rails!
- … potete fare quel che volete
- fate ciò che gli altri hanno già fatto bene, please!
- Refattorizziamo il Parser che abbiamo scritto prima!
A better Parser
Un (semplicissimo) webserver in NodeJS
my_web_server.js
- very “basic”
- Il programma non “termina” (Ctrl+C)
- MA …
- Servire file?
- check parameters?
- routes?
- Soluzione: EXPRESS !
Come installarlo? Con NPM! (quindi vediamo prima NPM)
NPM
npm install express --save
package.json:
Node Package Manager
https://www.npmjs.com/
A REST server with Express
(where is the GIF?)
(Using a Javascript framework for the first time)
Performance
L’approccio Event-based / Async IO paga?
Alcune comparison con PHP
Nota: HHVM è JIT + Async I/O
Vantaggi Svantaggi
● You already know JS
● Build tools in JS too! (npm, grunt,
express…)
● very fast JIT interpreter
● amazing community
● Some DBs (MongoDB) are written with
JS in mind
● real-time made easy
● support for high concurrency
● Corporate backup (Joyent)
● PaaS support NODE
● ES6
● NodeJS è ancora bleeding edge: altri
linguaggi più blasonati hanno decenni
di stabilità alle spalle
● (poor) IDE Support
● no strong typing
● nothing is really market standard
(neither Express)
○ that’s open source, baby
● Standard library is poor or too new or
unstable
● Synchronous Style is easier to think
about and program
● can you honestly love javascript?
Javascript Everywhere
Meetup Javascript &
NodeJSa Salerno!
Sabato 5 Marzo 2016
presso “Puntolingue” di fianco a Italo
primo binario, stazione FF.SS. Salerno
http://www.meetup.com/it-IT/JS-Salerno/events/228723943/
Risorse utili
- Tutorial “advanced” per Express:
https://scotch.io/tutorials/build-a-restful-api-using-
node-and-express-4
- ExpressJS : http://expressjs.com/
Build tools:
- Grunt http://gruntjs.com/- Gulp http://gulpjs.com/
Frontend:
- Bower http://bower.io/
Bye!
Questo gatto conosce l’event loop di NodeJS