to node.js

19
Introduc)on to Node.js

Upload: others

Post on 28-Mar-2022

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: to Node.js

Introduc)on  to  Node.js  

Page 2: to Node.js

Atwood's  Law:    any  applica)on  that  can  be  wri8en  in  JavaScript,  will  eventually  be  

wri8en  in  JavaScript.      

Source:  h8p://www.codinghorror.com/blog/2007/07/the-­‐principle-­‐of-­‐least-­‐power.html  

Page 3: to Node.js

3/19  

Page 4: to Node.js

4/19  

Page 5: to Node.js

What  is  Node.js?  

Allows  you  to  build  scalable  network  applica)ons  using  JavaScript  on  the  server-­‐side.    

•  asynchronous  i/o  framework  core  in  c++  on  top  of  v8  

•  rest  of  it  in  javascript  •  swiss  army  knife  for  

network  related  stuffs    •  can  handle  thousands  of  

concurrent  connec)ons  with  minimal  overhead  (cpu/memory)  on  a  single  process    

5/19  

Page 6: to Node.js

Async  vs  Sync  Blocking  Code  

Non  Blocking  Code  

6/19  

Page 7: to Node.js

Async  vs  Sync  Blocking  Code  

Non  Blocking  Code  

7/19  

Page 8: to Node.js

Callbacks  

8/19  

Page 9: to Node.js

Async  vs  Sync  

BLOCKING VS NON-BLOCKING

blocking

0snon-blocking

10s5s

0s 10s5s

fs.readFile('/etc/hosts', callback);fs.readFile('/etc/inetcfg', callback);

var callback = function(err, contents) { console.log(contents);}

9/19  

Page 10: to Node.js

Blocking  

10/19  

Page 11: to Node.js

Non  blocking  

11/19  

Page 12: to Node.js

Node.js  App  

•  Run  en)rely  in  a  single  thread  •  Passes  I/O  requests  to  the  event  loop,  along  with  callbacks  

•  Your  code  then:  – Goes  to  sleep  – Uses  no  system  resources  – Will  be  no)fied  via  callback  when  I/O  is  complete  

12/19  

Page 13: to Node.js

Event  (Loop)  

13/19  

Page 14: to Node.js

Event  Loop  

14/19  

Page 15: to Node.js

Event  Loop  

15/19  

Page 16: to Node.js

Example  

16/19  

Page 17: to Node.js

References  •  h8ps://www.udemy.com/lectures/understanding-­‐the-­‐nodejs-­‐event-­‐

loop-­‐91298  •  h8p://www.slideshare.net/gabriele.lana/introduc)on-­‐to-­‐nodejs  •  h8p://courseware.codeschool.com/node_slides.pdf  •  h8ps://speakerdeck.com/faisalabid/node-­‐dot-­‐js-­‐and-­‐you-­‐at-­‐codemo)on-­‐

roma  

17/19  

Page 18: to Node.js

More  in  depth  •  h8ps://blog.jcoglan.com/2013/03/30/callbacks-­‐are-­‐impera)ve-­‐promises-­‐

are-­‐func)onal-­‐nodes-­‐biggest-­‐missed-­‐opportunity/  •  h8p://www.futurealoof.com/posts/nodemodules-­‐in-­‐git.html  

18/19  

Page 19: to Node.js

Useful  stuff  •  h8ps://npmjs.org/  •  h8ps://github.com/visionmedia/express  •  h8ps://github.com/remy/nodemon  •  h8ps://github.com/node-­‐inspector/node-­‐inspector  •  h8ps://github.com/nodejitsu/forever  •  h8ps://github.com/kriskowal/q  •  h8ps://github.com/visionmedia/debug  •  h8ps://bitbucket.org/alemhnan/nestub/  

19/19