33
1

Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

1

Page 2: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Agenda• Quick'Intro'

• Node.js:'The'Beginning'

• What'Is'Node.js?

• Why'Use'Node.js?

• Installing'Node.js

2

Page 3: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Node.js is a platform for building applications

What%makes%Node.js%so%special?

• It$allows$you$to$run$JavaScript$outside$of$the$browser.

• Which$means$you$can$build$desktop$apps,$too!

• It's$really$fast$but..the$reason$for$that$is$a$li@le$complicated.

• Let's$give$it$a$li@le$context$by$understanding$how$and$why$Node$was$created.

3

Page 4: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

4

Page 5: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Ryan%Dahl

• Creator(of(Node.js

• He(first(presented(Node.js(at(a(JavaScript(Conference(in(2009(

• He(was(inspired(a<er(watching(a(Flickr(demo

5

Page 6: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

7

Page 7: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

8

Page 8: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

9

Page 9: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

10

Page 10: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.!

What%is%a%JavaScript%Engine?

• A#program#that#compiles#JavaScript#code#into#na6ve#machine#code.

• Some6mes#referred#to#as#a#compiler.

• Example#engines#include:

• V8#(Google)

• SpiderMonkey#(Mozilla)

• JavaScriptCore#(Apple)

• Chakra#(MicrosoG)

11

Page 11: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.!

What%is%a%JavaScript%Run1me?

• A#library#used#by#the#JavaScript#Engine#to#implement#func;ons#during#run;me#aka#execu;on#of#a#program.

• These#libraries#o@en#include#func;ons#for#input/output#and#memory#management.

• Example#run;mes#include:

• Node.js

• Browsers

12

Page 12: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Google&Chrome

• Uses%a%client'side%JavaScript%Run2me

• The%Run2me%is%part%of%the%browser

• Handles%tasks,%such%as:

13

Page 13: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Node.js

• Is$a$server%side$JavaScript$Run1me

• Is$installed$on$a$computer$or$server

• Handles$tasks,$such$as:

• Database$queries

• File$I/O$Requests

14

Page 14: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

15

Page 15: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Node%is%Event&DrivenRemember&the&JavaScript&Event2Handling&lecture?&

• When&this&event&happens,&do&this&ac'on.

• Example:&When&a&user&clicks&this&bu3on,&display&this&menu.

• This&is&considered&a&Client-Side&event.

16

Page 16: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Node%is%Event&Driven• Some&common&server,side&events,&include;

• connect

• abort

• open&

• close

• Example:&When&this&file&is&open,&append&the&date.

17

Page 17: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Node%is%Event&Driven• Node&is&always&listening&for&new&events

• Kind&of&like&a&nosy&neighbor&

18

Page 18: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Node%is%Event&Driven• When&Node&recognizes&an&event,&it&sends&the&rela4ng&ac4on&off&to&process,&then&creates&a&callback.

• A&callback&is&just&that,&Node&calls&back&that&ac4on,&so&it&can&answer&another&event.&

• Example:&When&this&file&is&open,&append&the&date...brb...Ok,&now&close&the&file.

19

Page 19: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

20

Page 20: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Node%is%Non$Blocking• Non%Blocking,opera1ons,are,some1mes,referred,to,as,Asynchronous,opera1ons,

• Other,code,will,execute,while,Node,waits,for,the,asynchronous,opera1on,to,complete

21

Page 21: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

vs#Blocking• Blocking*opera/ons*are*some/mes*referred*to*as*Synchronous*opera/ons*

• No*other*code*can*execute*un/l*the*synchronous*opera/on*completes

• If*the*opera/on*is*slow,*this*can*be*an*issue

22

Page 22: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

23

Page 23: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Node%is%Single,Threaded• A#thread#is#a#single#computer#process

• Node's#main#Event#Loop#runs#in#a#single#thread

• Events#and#Callbacks#are#queued#in#the#order#they#are#received

24

Page 24: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Event&Loop&Example• A#web#request#is#received

• Node#executes#the#handler#for#that#request

• The#handler#ini9ates#a#database#query,#with#a#callback

• Node#is#free#and#able#to#handle#other#requests

• The#database#query#ends,#and#Node#is#no9fied#(event)

• Node#adds#the#callback#to#the#queue

• Node#executes#the#handler#a@er#processing#any#events#before#it#in#the#queue

25

Page 25: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback
Page 26: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

26

Page 27: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

On#It's#Own,#Node#is#NOT...• a#web#framework

• so,#it's#not#a#JavaScript#version#of#Ruby#on#Rails

!27

Page 28: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

HOWEVER,(when(paired(with...• web%frameworks,%such%as%Express%or%AngularJS

• you%can%create%full5fledged%web%applica;ons

28

Page 29: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

You$can$ALSO$create...• desktop)applica.ons

• video)games

• chat)rooms

• remote)controls)for)toy)cars)!

29

Page 30: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

30

Page 31: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Installa'on)Steps1. Go%to%h(ps://nodejs.org

2. Click%the%LTS%download%bu(on

3. Open%Installer

4. Follow%prompts%to%complete%the%installaCon

31

Page 32: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Basic&Node&Terminal&Commands

• node

• opens'an'interac-ve'shell'where'you'can'execute'JavaScript'code

• node file_name.js

• executes'JavaScript'code'that'is'in'a'file'

• node -v'

• displays'the'version'of'Node'installed'on'your'computer

32

Page 33: Agendacomet.lehman.cuny.edu/sfulakeza/su19/tpp/slides/Day 10/revised-no… · Event&Loop&Example • A#web#request#is#received • Node#executes#the#handler#for#that#request • The#handler#ini9ates#a#database#query,#with#a#callback

Exercise(#1:!Hello!Node!

• Using'the'Node'interac1ve'shell,'output'Hello Node!'in'the'terminal

• Save'a'Hello Node!'script'in'a'file'and'execute'that'file'in'the'Terminal

33