1 / 32

var talk = { title: " Node.JS ”, subTitle : “ What’s All the Fuss About? ” }

var talk = { title: " Node.JS ”, subTitle : “ What’s All the Fuss About? ” }. Agenda. Origins Isn’t server programming a solved problem? Inside Node Async IO and the Evented Model Hands on Let‘s write some code The real world Packages and popular frameworks. ORIGINS.

cynara
Download Presentation

var talk = { title: " Node.JS ”, subTitle : “ What’s All the Fuss About? ” }

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. vartalk = { title: "Node.JS”, subTitle: “What’s All the Fuss About?” }

  2. Agenda • Origins Isn’t server programming a solved problem? • Inside Node Async IO and the Evented Model • Hands on Let‘s write some code • The real world Packages and popular frameworks

  3. ORIGINS

  4. Evolution of Socket Servers • In the beginning was… CGI one new process per request • Worker Pool Model dispatch requests to a pool of persistent workers • The C10k problem 10 thousand concurrent connections The race for high throughput and low latency.

  5. Thread Pool Model concurrency = # threads (or processes) thread pool request queue responses thread 1 thread 2 thread 3 thread 4 thread n

  6. Efficiency of a Worker Thread process results route, parse request form db query parse db result form web service query form response wait… wait… wait… wait… wait… wait… wait… wait… wait… wait… wait… wait… db query web service query log to disk

  7. Relative I/O Latency

  8. Is there a better way?

  9. Asynchronous (non-blocking) I/O • Initiate I/O operation and return, don’t block • Perform other tasks instead of idling • When I/O completes, process result using an event notification interface • Popular web servers: nginx and lighttpd

  10. Inside node

  11. Node.JS in five words Evented I/O for V8 JavaScript A Great Networking Tool

  12. Evented Model user space internal thread pool event queue event loop single-thread network file system other I/O done

  13. What is Node.JS made of? node standard library http(s), net, stream, fs, events, buffer JS node bindings JS / C++ V8 JavaScript VM libuv thread pool event loop async I/O c-ares async DNS http_parser OpenSSL zlib, etc. C/C++

  14. Hands on

  15. Async Callbacks - Look Familiar? setTimeout(function(){     console.log("time's up") },1000); console.log('hello') while(true){} what gets printed now?

  16. Simple Socket Server var net = require('net') var server =net.createServer(function(socket){ socket.write('hello\n') socket.end() }) server.listen(9898)

  17. Events –Listeners and Emitters varserver =net.createServer(function(socket){ socket.on('data',function(data){         console.log(data.toString()) }) socket.on('end',function(){         console.log('client disconnected') }) }).listen(9898)

  18. Simple HTTP Server var http = require('http') http.createServer(function(req, res){ res.writeHead(200,{'Content-Type':'text/plain'}) res.end("hello\n") }).listen(9090) How do we read the request body?

  19. Making HTTP Requests var http = require('http') varreq=http.request({     host:'jssaturday.com',     path:'/sofia' },function(res){     console.log(res.statusCode) res.on('data',function(data){         console.log(data.toString()) }) }) req.end()

  20. Simple HTTP Forwarding Proxy • How difficult would it be to write a local forwarding proxy?

  21. Simple HTTP Forwarding Proxy var http = require('http') http.createServer(function(req, res){ req.pipe(http.request({         host:req.headers.host,         path: req.url,         headers:req.headers },function(xRes){ res.writeHead(xRes.statusCode,xRes.headers) xRes.pipe(res) })) }).listen(8080)

  22. Challenges • Debugging • why is my stack trace so short • exception handling • Non-linear code • Nesting • Requires shift of programming paradigm • Blocks on CPU • Beware of CPU intensive tasks • Run multiple nodes or child processes

  23. Benefits • Async I/O made easy • Single-thread simplifies synchronization • One language to rule them all • Very active community • Multi-platform

  24. The real world

  25. Modules base64.js app.js var b64 = require('./base64') var a = b64.toBase64('JSSaturday') var encoding ='base64‘ // locals are private exports.toBase64=function(s){ returnnew Buffer(s).toString(encoding) }

  26. Node Package Management • NPM • install and publish packages • upgrade, search, versioning • npmjs.org • browse popular packages • publish your own

  27. Node.JS Resources • nodejs.org • which version to use? • Event X: stable (0.8.x, 0.10.x) • Odd X: unstable (0.9.x, 0.11.x) • Documentation: nodejs.org/api • Playing with the command line REPL • Build from source: github.com/joyent/node

  28. expressjs: web app framework • Node.JS is powerful • full control over HTTP server • but most of the time you’ll use a web framework • Web app frameworks likes ExpressJS provide • Request Routing • Body and Parameter Parsing • Session and Cookie Management • Templates • Static File Serving, Logger and many more

  29. ExpressJS – hit counter var express = require('express') var app = express(); app.use(express.cookieParser()); app.use(express.cookieSession({secret:"dG9wc2VjcmV0"})); app.use(function(req, res){ varsess=req.session sess.hits=sess.hits||0 sess.hits++ res.json({ visits:sess.hits}) }); app.listen(80)

  30. Questions? res.setHeader(“Content-Type”, “text/plain”) res.write(“valentin.kostadinov@gmail.com\n”) res.end(“Bye!”)

  31. Expect very soon: SharePoint Saturday! • Saturday, June 8, 2013 • Same familiar format – 1 day filled with sessions focused on SharePoint technologies • Best SharePoint professionals in the region • Registrations will be open next week (15th)! • www.SharePointSaturday.eu

  32. Thanks to our Sponsors: Diamond Sponsor: Platinum Sponsors: Gold Sponsors: Swag Sponsors: Media Partners:

More Related