node.js deep dive - siliconindia · pdf file1 18th august,2012 presentation material node.js...
TRANSCRIPT
![Page 1: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/1.jpg)
1 18th August,2012
Presentation Material
Node.JS Deep Dive Training Deck By
Prasoon Kumar Senior Technical Architect Just Dial
![Page 2: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/2.jpg)
What is node?
• Server-side JavaScript framework
• Written using Google V8 engine
• Uses CommonJS module system
• Has the ECMASCript5 features
• Non-blocking from ground up
• Servers are normally thread based but Node.JS is “Event” based. Node. JS serves each request in an Evented loop that can able to handle simultaneous requests.
![Page 3: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/3.jpg)
Who is using it?
• Sponsored by
• Microsoft (native support in Azure)
• Yahoo
• eBay
• Many, many smaller companies
![Page 4: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/4.jpg)
Threads Asynchronous Event-driven
Lock application / request with listener-workers threads
only one thread, which repeatedly fetches an event
Using incoming-request model Using queue and then processes it
multithreaded server might block the request which might involve multiple events
manually saves state and then goes on to process the next event
Using context switching no contention and no context switches
Using multithreading environments where listener and workers threads are used frequently to take an incoming-request lock
Using asynchronous I/O facilities (callbacks, not poll/select or O_NONBLOCK) environments
Threads VS Event-driven
![Page 5: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/5.jpg)
Node is…
![Page 6: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/6.jpg)
Node layers
Node standard library
Node bindings
V8
Thread Pool libeio
Event pool libev
JavaScript
C
![Page 7: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/7.jpg)
What? Event looping…
![Page 8: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/8.jpg)
Non-blocking IO
• The philosophy behind node is that system interactions should be non blocking.
Instead of this
Use this var fs = require('fs'); // Import the 'fs' module
// Read the file asynchronously
fs.readFile(__dirname + '/example.file', 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
console.log(data); // print it! });
var fs = require('fs'); // Import the 'fs' module var data = fs.readFileSync(__dirname + '/example.file','utf8'); console.log(data); // print it!
![Page 9: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/9.jpg)
Installation
[prasoonk@prasoonk node-v0.8.6]$ node –version
v0.8.6
[prasoonk@prasoonk node-v0.8.6]$ node
2+3
5 process.versions
{ http_parser: '1.0', node: '0.8.6', v8: '3.11.10.17', ares: '1.7.5-DEV', uv: '0.8',
zlib: '1.2.3', openssl: '1.0.0f' }
./configure --prefix=/opt/node make sudo make install
![Page 10: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/10.jpg)
What are packages?
• Ruby has Gems • Python has PyPI
• PHP has PEAR (terrible)
Node.js has Packages
![Page 12: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/12.jpg)
npm commands
• ls [filter] – installed, stable, @1.0
• Install package_name@version branch
• rm
![Page 13: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/13.jpg)
NPM – Node Package
Manager
![Page 14: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/14.jpg)
API Tour
• Platform – process – file system – Networking
• net, dgram, http, tls (ssl)
• Utility – console – util
• Buffer • Event Emitter • Timer
![Page 15: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/15.jpg)
Process
• Process – PID, platform, memory usage
• Child_process –
– spawn and kill processes,
– Execute commands and
– Pipe their outputs
![Page 16: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/16.jpg)
API Tour: File System
• fs
• path
![Page 17: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/17.jpg)
Hello World
server.js File:
var http = require('http');
http.createServer(function (req, res) { res.writeHead(200, {'Content-Type':'text/plain'}); res.end('Hello, world! ');
}).listen(80);
![Page 18: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/18.jpg)
DERBY
Realtime apps
Real easy
![Page 19: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/19.jpg)
All app code client and server
Real-time synced
• Responsive
• Offline
• No glue code
• SEO friendly
![Page 20: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/20.jpg)
• C:\src\hello> npm install derby
DEMO
![Page 21: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/21.jpg)
http://twitter.com/prasoonk
http://prasoonk.wordpress.com
http://slideshare.net/prasoonk
Thanks!
Visit Me @
![Page 22: Node.JS Deep Dive - SiliconIndia · PDF file1 18th August,2012 Presentation Material Node.JS Deep Dive Training Deck By Prasoon Kumar Senior Technical Architect Just Dial](https://reader031.vdocuments.us/reader031/viewer/2022030507/5ab5b3da7f8b9a86428cfaa6/html5/thumbnails/22.jpg)
Quick References
• https://github.com/joyent/node/wiki Github Node Wiki
• http://nodemanual.org - API docs, tutorials and live code samples
• http://c9.io Cloud9 IDE (use github id) • http://docs.nodejitsu.com • http://howtonode.org/ Tutorials • https://npmjs.org/ NPM registry • https://github.com/WindowsAzure/azure-sdk-
for-node
Quick References