to node.js
TRANSCRIPT
![Page 1: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/1.jpg)
Introduc)on to Node.js
![Page 2: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/2.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/3.jpg)
3/19
![Page 4: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/4.jpg)
4/19
![Page 5: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/5.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/6.jpg)
Async vs Sync Blocking Code
Non Blocking Code
6/19
![Page 7: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/7.jpg)
Async vs Sync Blocking Code
Non Blocking Code
7/19
![Page 8: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/8.jpg)
Callbacks
8/19
![Page 9: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/9.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/10.jpg)
Blocking
10/19
![Page 11: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/11.jpg)
Non blocking
11/19
![Page 12: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/12.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/13.jpg)
Event (Loop)
13/19
![Page 14: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/14.jpg)
Event Loop
14/19
![Page 15: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/15.jpg)
Event Loop
15/19
![Page 16: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/16.jpg)
Example
16/19
![Page 17: to Node.js](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/17.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/18.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032903/6240e47fe702fc65025395d5/html5/thumbnails/19.jpg)
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