1 / 26

var title = “ Node.JS - Blurring the Line Between Client and Server ”;

var title = “ Node.JS - Blurring the Line Between Client and Server ”;. $(this).attr(“title”, title); $(this).data( { font: ‘Segoe UI’ , size: ‘30pt’ , maxLines : ‘3 lines max’ } );. Agenda. Why Node.JS? Why yet another server framework? What is it?

azia
Télécharger la présentation

var title = “ Node.JS - Blurring the Line Between Client and Server ”;

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. var title = “Node.JS - Blurring the Line Between Client and Server”; $(this).attr(“title”, title); $(this).data({ font: ‘Segoe UI’, size: ‘30pt’, maxLines: ‘3 lines max’ });

  2. Agenda • Why Node.JS? • Why yet another server framework? • What is it? • Asynchronous IO and the Evented Model • Hands On • Let’s write some code • How to node • Package, frameworks and tools • Latest features • Q&A

  3. JavaScript on the Server Cons • It’s JavaScript – is it even a real language? • Async programming – can be unfamiliar and hard • Nested callback hell • Single process doesn’t scale across cores • More? Pros • It’s JavaScript – it’s the best! • It’s fast and easy to scale • One language to rule them all • Share code across client/server • Never write serialization code again – it’s all JSON • Never debug multithreaded code again • More?

  4. ORIGINS Why yet another server framework?

  5. Evolution of Socket Servers • In the beginning was CGI • A new process per request • Worker Pool Model • Dispatch requests to persistent worker pool • To infinity and beyond • The C10k problem • The race for high throughput and low latency

  6. Thread Pool Model concurrency = # threads thread pool thread 1 request queue responses thread 2 thread 3 thread 4 thread n

  7. Worker ThreadEfficiency 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

  8. Relative I/O Latency next room ~5m across the street ~20m next block ~400m Earth circumference distance to the Moon

  9. IS THERE A BETTER WAY?

  10. Asynchronous I/O • Start I/O and return (non-blocking) • Perform other tasks • When I/O completes, process the result • Handle requests in a single thread • Popular examples: nginx, lighttpd

  11. Node.JS in 5 words Evented I/O for V8 JavaScript

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

  13. Async Callbacks – Look Familiar? setTimeout(function(){ console.log("time's up") },1000); console.log('hello') while(true){}

  14. HANDS ON Let’s write some code

  15. “Hello, World!” var http = require('http') http.createServer(function(req, res){ res.writeHead(200,{'Content-Type':'text/plain'}) res.end("hello\n") }).listen(9090)

  16. Static HTTP Server var http = require('http') var fs = require('fs') http.createServer(function(req, res){ fs.readFile('index.html',function(err, data){ if(err){ res.writeHead(500) res.end() }else{ res.end(data) } }) }).listen(9090)

  17. Let’s code Writing a simple blog

  18. Module System app.js base64.js var b64 = require('./base64') var a = b64.toBase64('JQueryBulgaria') var encoding ='base64‘ // locals are private exports.toBase64=function(s){ returnnew Buffer(s).toString(encoding) }

  19. Error Handling • Asynchronous Callbacks and Exceptions • Dude, where is my stack? • Cannot debug across event loop iterations • Async callback error code convention • First callback parameter is error object fs.readFile(path,function(err, file){ if(err){ // handle error } //... })

  20. “Do, or do not. There is no try.” • Reconsider the conventional wisdom of exception handling • Exceptions are cleaner, more elegant and… wrong • Hidden control flow and Corrupt State • Just because you don’t see a code path doesn’t mean it doesn’t exist • New generation of languages shun exceptions (like Go)

  21. Callback Hell – the Modern GOTO

  22. AvoidingThe Callback Pyramid of Doom • Keep it shallow • Name your anonymous functions • Avoid nesting • Use a functional programming helper module • async, underscore (both server and client) • Other alternatives • Promises • Fibers

  23. Web App Frameworks • Node.JS is powerful • Full control over HTTP server • But most of the time you’ll use a web framework • Web App Frameworks like ExpressJS provide: • Routing • Body parsing • Session and Cookie Management • Templating • Static File Server, Logger and many more

  24. 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)

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

  26. Thanks to our Sponsors: Diamond Sponsor: Gold Sponsors: Silver Sponsors: Technological Partners: Bronze Partners: Swag Sponsors: Media Partners:

More Related