Your SlideShare is downloading. ×
A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

5,461
views

Published on

Published in: Technology

0 Comments
12 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,461
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
173
Comments
0
Likes
12
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript

    • 1. A language for the Internet Why JavaScript and Node.js is right for Internet ApplicationsTom Hughes-Croucher@sh1mmer
    • 2. Me(@sh1mmer)
    • 3. Scalable Server-Side Code with JavaScriptNode Up and Running Tom Hughes-Croucher
    • 4. A brief aside.A small lecture on biology
    • 5. The common tree shrew
    • 6. Diana Monkey
    • 7. Back to your feature presentation.
    • 8. Internet?
    • 9. She’s called Eleanor
    • 10. More features More usersMore devices More data
    • 11. Cost Stuff
    • 12. How do we cope withthe increase in demand?
    • 13. Internet Applications
    • 14. How about search?
    • 15. Browser Server Spidering... The Web
    • 16. Would take forever!
    • 17. Browser Front-end ServerComputation Database
    • 18. Computational ComputingClient → Server Computation
    • 19. Internet ComputingClient → Server Server → DB Computation Computation
    • 20. “Traditional” Approach
    • 21. Server
    • 22. Request
    • 23. U L LF
    • 24. Event-driven Approach
    • 25. Place-holder
    • 26. Shared WorkResources
    • 27. Welcome to Node.js
    • 28. Node.js?• Server Side JavaScript runtime• Built on top of V8 JavaScript engine from Google Chrome• Non-blocking I/O APIs• Easy to extend APIs and modules
    • 29. $Enki:~ $ node
    • 30. $Enki:~ $ node>3>2>1false> true == 1true> true === 1false
    • 31. > console.log(Hello World);Hello World> .help.clear Break, and also clear the local context..exit Exit the prompt.help Show repl options> .clearClearing context...> .exitEnki:~ $
    • 32. Enki:~ $ node> var foo = "bar";> foo;bar> .clearClearing context...> fooReferenceError: foo is not defined at [object Context]:1:1 at Interface.<anonymous> (repl:98:19) at Interface.emit (events:27:15) at Interface._ttyWrite (readline:295:12) at Interface.write (readline:132:30) at Stream.<anonymous> (repl:79:9) at Stream.emit (events:27:15) at IOWatcher.callback (net:489:16)
    • 33. var http = require(http);http.createServer(function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);}).listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/);
    • 34. var http = require(http);//include the http library
    • 35. http.createServer(function (req, res) {}).listen(8124, "127.0.0.1");//create an http server//when ‘stuff’ happens call this anonymous function//listen on port 8124 of the IP 127.0.0.1
    • 36. http.createServer(function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);})//when ‘stuff’ happens my function fires//I get a request object and a response object//I write to the response object header//HTTP status 200 and content-type ‘text/plain’//close the response with the body://Hello World
    • 37. console.log(Server running at http://127.0.0.1:8124/);//write Server is running at http://127.0.0.1:8124///to the console
    • 38. Why is Node.js suited to Internet Apps?
    • 39. What is the event loop?
    • 40. Multi-tasking,one thing at a time.
    • 41. var http = require(http);server = http.createServer();server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);});server.listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/);
    • 42. var http = require(http);server = http.createServer();server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);});server.listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/); Step 1. Evaluate Main
    • 43. var http = require(http);server = http.createServer();server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);});server.listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/); Step 1. variables: http -> http module server -> http server listeners: server.request -> function
    • 44. var http = require(http); server = http.createServer();* server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn); }); server.listen(8124, "127.0.0.1"); console.log(Server running at http://127.0.0.1:8124/); Step 2. Event Loop
    • 45. var http = require(http); server = http.createServer();* server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn); }); server.listen(8124, "127.0.0.1"); console.log(Server running at http://127.0.0.1:8124/); Step 2. Do we have active listeners? listeners: server.request -> function Yes! Wait for listeners.
    • 46. var http = require(http);server = http.createServer();server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);});server.listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/); Step 3. Event Calls
    • 47. var http = require(http);server = http.createServer();server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);});server.listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/); Step 3. request is called. Since listeners: server.request -> function Call function
    • 48. var http = require(http);server = http.createServer();server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);});server.listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/); Step 3. Loop! (go to Step 2.)
    • 49. Breaking the event loop
    • 50. EE = require(events).EventEmitter;ee = new EE();die = false;ee.on(die, function() { die = true;});setTimeout(function() { ee.emit(die);}, 100);while(!die) {}console.log(done);
    • 51. Why non-blocking matters
    • 52. var result =db.query("select * from T");// use result
    • 53. What are wewaiting for?
    • 54. "Blocking" is as bad as stopping
    • 55. Event Loop vs. Threads
    • 56. PHP8mb 8gb ram 8000/8 = 1000~1000 users per machine
    • 57. Node.js TCP = 2kb HTTP = 6kb 8gb ram 8000/0.006 = 1.3m1.3m/2 ~ 650k users
    • 58. Apache vs NGINXconcurrency × reqs/sechttp://blog.webfaction.com/a-little-holiday-present
    • 59. Apache vs NGINXconcurrency × memoryhttp://blog.webfaction.com/a-little-holiday-present
    • 60. Node.js is designed forcommunication, just like your applications
    • 61. Thanks!
    • 62. Follow me @sh1mmerTom Hughes-Croucher@sh1mmer