20
The Sexy Platform Michele Nasti Napoli Dev Day - Rework - 24/02/2016

Node.JS: The sexy platform

  • Upload
    devday

  • View
    188

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Node.JS: The sexy platform

The Sexy Platform

Michele NastiNapoli Dev Day - Rework - 24/02/2016

Page 2: Node.JS: The sexy platform

Chi sono?

● http://michelenasti.com

● Twitter (@micnasti)

● Linkedin

● Facebook

● ... e tutti gli altri servizi on line

(Stack Overflow, Github, ....)

Page 3: Node.JS: The sexy platform

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.

Page 4: Node.JS: The sexy platform

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

Page 5: Node.JS: The sexy platform

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}

Page 6: Node.JS: The sexy platform

“vuttamm e’ mman:”un Parser in JS

Page 7: Node.JS: The sexy platform

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” ⇒

Page 8: Node.JS: The sexy platform

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!

Page 9: Node.JS: The sexy platform

A better Parser

Page 10: Node.JS: The sexy platform

Un (semplicissimo) webserver in NodeJS

Page 11: Node.JS: The sexy platform

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)

Page 12: Node.JS: The sexy platform

NPM

npm install express --save

package.json:

Node Package Manager

https://www.npmjs.com/

Page 13: Node.JS: The sexy platform

A REST server with Express

(where is the GIF?)

Page 14: Node.JS: The sexy platform

(Using a Javascript framework for the first time)

Page 15: Node.JS: The sexy platform

Performance

L’approccio Event-based / Async IO paga?

Alcune comparison con PHP

Nota: HHVM è JIT + Async I/O

Page 16: Node.JS: The sexy platform

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?

Page 17: Node.JS: The sexy platform

Javascript Everywhere

Page 18: Node.JS: The sexy platform

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/

Page 19: Node.JS: The sexy platform

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/

Page 20: Node.JS: The sexy platform

Bye!

Questo gatto conosce l’event loop di NodeJS