build app with nodejs - ywc workshop

38
Node.js For Beginner Kao #YWC9 Software Engineer @ jitta.com

Upload: sarunyhot-suwannachoti

Post on 21-Jan-2018

226 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Build App with Nodejs - YWC Workshop

Node.js For BeginnerKao #YWC9

Software Engineer @ jitta.com

Page 2: Build App with Nodejs - YWC Workshop

What is Node.js• Created by Ryan Dahl in 2009

• Server-side JavaScript

• Uses V8 is an open source JavaScript engine developed by Google. Its

• written in C++ and is used in Google Chrome Browser

• Event-driven with Non-blocking I/O

Page 3: Build App with Nodejs - YWC Workshop

The Essence of Node• JavaScript on the Server

• More than server-side JavaScript

• Fabulous framework

• Asynchronous programming

• Module-driven development

Page 4: Build App with Nodejs - YWC Workshop

It’s is JavaScript

• Easy to learn

• Lets you unify your client/server logic

• Productivity

• Already in the Enterprise (Paypal, Netflix, Walmart, IBM)

Page 5: Build App with Nodejs - YWC Workshop

More than server-side JavaScript

• Web server

• Robot controller (tessel.io)

• Command line application

• Proxy server

• Music machine

• Desktop application tooling: NW.js, Electron

Page 6: Build App with Nodejs - YWC Workshop

Module-driven development

• Node Package Manager (NPM)

• 250,000++ total packages

• 160,000,000 downloads per day

• Simplicity

• Decoupled and reusable coding

Page 7: Build App with Nodejs - YWC Workshop

Module-driven development

• npm focused on module-driven development

• ES6 Modules

• Module-driven development for the browser

• bower install moment —save

• npm install moment —save

Page 8: Build App with Nodejs - YWC Workshop

Desktop Application(torrent)

Page 9: Build App with Nodejs - YWC Workshop

Desktop Application (editor)

Page 10: Build App with Nodejs - YWC Workshop

Game Dev Tycoon

Page 11: Build App with Nodejs - YWC Workshop

Fabulous Framework• Express

• Restify (for building REST API)

• Hapi (configuration-centric framework)

• Sails (fast production-ready)

• Meteor (realtime application)

Page 12: Build App with Nodejs - YWC Workshop

Asynchronous programming

Page 13: Build App with Nodejs - YWC Workshop

5s0s 10s

BLOCKING

Page 14: Build App with Nodejs - YWC Workshop

5s0s 10s

NON-BLOCKING

Page 15: Build App with Nodejs - YWC Workshop

Create node module

Page 16: Build App with Nodejs - YWC Workshop

What is Node made of?

Page 17: Build App with Nodejs - YWC Workshop

Event Loop

Page 18: Build App with Nodejs - YWC Workshop

V8 JavaScript Engine• V8 is Google's open source JavaScript engine

• V8 implements ECMAScript as specified in ECMA-262

• V8 is written in C++ and is used in Google Chrome, the open source browser from Google

• V8 can run standalone, or can be embedded into any C++ application

Page 19: Build App with Nodejs - YWC Workshop

Create Event Emitter

Page 20: Build App with Nodejs - YWC Workshop

Stream and Buffer

Page 21: Build App with Nodejs - YWC Workshop

Stream

• A sequence of data made available over time

Page 22: Build App with Nodejs - YWC Workshop

Buffer

• A temporary holding spot for data being moved from one place to another

Hello WorldBuffer Buffer

Page 23: Build App with Nodejs - YWC Workshop

StreamLorem ipsum dolor sit

amet, consectetur adipisicing elit, sed do

eiusmod tempor incididunt ut labore et

dolore magna aliqua. Ut enim ad minim veniam,

quis nostrud exercitation ullamco laboris nisi ut

aliquip ex ea commodo consequat. Duis aute

irure dolor in

lowercase.txt

LOREM IPSUM DOLOR SIT AMET,

CONSECTETUR ADIPISICING ELIT, SED DO EIUSMOD TEMPOR

INCIDIDUNT UT LABORE ET DOLORE MAGNA ALIQUA. UT ENIM AD MINIM VENIAM, QUIS

NOSTRUD EXERCITATION

ULLAMCO LABORIS NISI

uppercase.txt

stream

Page 24: Build App with Nodejs - YWC Workshop

StreamProcess

Stream

roL e m

Page 25: Build App with Nodejs - YWC Workshop

StreamProcess

Stream

Lorembuffer

LOREMbuffer

Page 26: Build App with Nodejs - YWC Workshop

StreamProcess

Stream

ipsumbuffer

IPSUMbuffer

Page 27: Build App with Nodejs - YWC Workshop

StreamProcess

Stream

LOREM IPSUM

Page 28: Build App with Nodejs - YWC Workshop

StreamLOREM IPSUM DOLOR

SIT AMET, CONSECTETUR

ADIPISICING ELIT, SED DO EIUSMOD TEMPOR

INCIDIDUNT UT LABORE ET DOLORE MAGNA ALIQUA. UT ENIM AD MINIM VENIAM, QUIS

NOSTRUD EXERCITATION

ULLAMCO LABORIS NISI

uppercase.txt

Page 29: Build App with Nodejs - YWC Workshop

What is pipe

• Connecting two streams by writing to one stream

• Let you easily read data from a source and pipe it to a destination

Page 30: Build App with Nodejs - YWC Workshop

Another Stream

PipeProcess

Readable Streampipe

Process

Hello

Writeable Stream

Page 31: Build App with Nodejs - YWC Workshop

Write Stream

Page 32: Build App with Nodejs - YWC Workshop

Build App with Node

Page 33: Build App with Nodejs - YWC Workshop

Browser

Request

Response

WebServer

Page 34: Build App with Nodejs - YWC Workshop

Browser

Request

Response

WebServer

HTTP

HTTP

Page 35: Build App with Nodejs - YWC Workshop

Browser

Request

Response

WebServer

HTTP

HTTP

Javascript

Page 36: Build App with Nodejs - YWC Workshop

Browser

Request

Response

WebServer

HTTP

HTTP

Javascript

Javascript

Page 37: Build App with Nodejs - YWC Workshop

What’s JavaScript on Server-side

• Read and write file

• Connect with databases

• Build web server

• Asynchronous task queue/job queue

Page 38: Build App with Nodejs - YWC Workshop

Create http server