Introduction to node.js

3,860 views
3,684 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,860
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
80
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Introduction to node.js

  1. 1. Introduction to Node.js By Arun Kumar Arjunan, Product Architect, Chronus
  2. 2. AGENDA• About Node.js• Programming in Node.js• Web Frameworks• Node.js internals• Popularity of Node.js• Downside of Node.js
  3. 3. ABOUT NODE.JSWhat isNode.js?
  4. 4. ABOUT NODE.JS Node.jsApplications
  5. 5. node.js KO
  6. 6. NIDE
  7. 7. Chess@home
  8. 8. GITJSGitJS is a pure JavaScript implementationOf Git.Example Commands:• git.js log• git.js branch• git.js show SHA
  9. 9. NODECHAT
  10. 10. DEFINITION Node.js is an event-driven I/O server-sideJavaScript environment
  11. 11. AGENDA• About Node.js Programming in Node.js• Web Frameworks• Node.js internals• Popularity of Node.js• Downside of Node.js
  12. 12. HELLO WORLD PROGRAMNode.JS is a command line tool $ node hello-console.js
  13. 13. HTTP SERVER
  14. 14. FILE SYSTEM
  15. 15. OTHER LIBRARIES• Timers • DNS• Events • REPL• Buffers • Child Processes• Streams • ZLIB• Crypto • OS• TLS/SSL
  16. 16. NODE IS EVENT DRIVENDid you notice that the previous examples use Event Driven technique?
  17. 17. NODE IS EVENT DRIVEN
  18. 18. NODE IS EVENT DRIVEN
  19. 19. NODE IS EVENT DRIVEN$ ab –n 200 -c 50 http://localhost:8080/
  20. 20. NOW IN RUBY require rubygems require mongrel class HelloWorldHandler < Mongrel::HttpHandler def process(request, response) sleep a2 response.start(200) do |head,out| head["Content-Type"] = "text/plain" out.write(“Hello World”) end end end h = Mongrel::HttpServer.new("127.0.0.1", "1337") h.register("/", HelloWorldHandler.new) h.run.join$ ab –n 200 -c 50 http://localhost:1337/
  21. 21. EVENT DRIVEN VS THREADINGLets compare Event Driven Programming& Thread based programming!!! • Some theory proves Threading model is better • Some proves Event Driven Programming is better
  22. 22. EVENT DRIVEN VS THREADINGLets take an example that is relevant to us!!!
  23. 23. APACHE VS NGINX
  24. 24. APACHE VS NGINX
  25. 25. APACHE VS NGINXThe difference?Apache uses one thread perconnection.NGINX doesn’t use threads. It usesan event loop
  26. 26. Disadvantages of Threading• Context switching is not free• Execution stacks take up memory• For massive concurrency, cannot use an OS thread for each connection.
  27. 27. AGENDA• About Node.js• Programming in Node.js Web Frameworks• Node.js internals• Popularity of Node.js• Downside of Node.js
  28. 28. NODE.JS COMMUNITY Node.jshas excellent community
  29. 29. NODE PACKAGE MANAGER NPMIts like bundler for Ruby Gems
  30. 30. AROUND 60 TEMPLATE ENGINESasyncEJS, bake, bind-js, blue, CoffeeKup, combyne.js,doT.js, dust, Eco, ejs, haml.js, haml-js, jshtml, jqtpl,Jade, jazz, JinJS, JSON Template, jm, jsdom, less.js,Liquor, minimal.js, Mu, nTPL, nodejs-meta-templates,normal-template, nun, node.magic_dom, node-template,node-properties, Parrot, PURE, stencil, Node-T, Swig,Templ8, template.node.js, tmpl-node, TwigJS, weld
  31. 31. WEB FRAMWEORKS1. Geddy2. ExpressJS3. RailwayJS
  32. 32. RAILWAYJSQuick guide to RailwayJS Its like Rails itself
  33. 33. ROUTES IN RAILWAYJS
  34. 34. CONTROLLERS IN RAILWAYJS
  35. 35. MODELS IN RAILWAYJS
  36. 36. AGENDA• About Node.js• Programming in Node.js• Web Frameworks• Node.js Internals• Popularity of Node.js• Downside of Node.js
  37. 37. NODE.JS ARCHITECTURE
  38. 38. ABOUT V8• V8 team is led by Lars Bak• Lars Bak was the technical lead behind HotSpot (Sun’s Java VM).• HotSpot improved Java’s performance 20x times• Before HotSpot Lars Bak worked on a Smalltalk VM• V8 uses hidden classes
  39. 39. UNDERSTANDING V8 Lets try tounderstand V8
  40. 40. UNDERSTANDING V8• It is used to run JavaScript• Can be embedded into any application• Can expose C methods & Objects using JS interface
  41. 41. USING V8
  42. 42. USING V8String::New(“a = 1; b = 2; a + b”)$ ./hello-worldString::New(ARGV[1])$./hello-world “a = 1; b = 2; a + b”String::New(fread(ARGV[1]))$./hello-world hello-world.js
  43. 43. NODE.JS ADVANTAGESSo Node.Js is nothing but a set of libraries using V8 engine
  44. 44. AGENDA• About Node.js• Programming in Node.js• Web Frameworks• Node.js Internals• Popularity of Node.js• Downside of Node.js
  45. 45. NODE.JS ADVANTAGES• Server side Javascript• Javascript is dynamic• Event based• Learning curve is less• Suited for event driven programming• Uses V8 which relatively faster• Uses CommonJS module spec
  46. 46. NODE.JS ADVANTAGESIs the server side JavaScriptprogramming new?
  47. 47. SERVER SIDE JS FRAMEWORKS• Helma - Rhino• AppJet - Rhino• Aptana Jaxer - SpiderMonkey• CouchDB - SpiderMonkey
  48. 48. NODE.JS ADVANTAGES Is the Event Drivenprogramming new?
  49. 49. EVENT DRIVEN PROGRAMMING LANGUAGES• E Programming language• Event Driven MVC Framework for SmallTalk• AmbientTalk
  50. 50. Similar Frameworks• Event Machine for Ruby• Libevent for C• Twisted for Python
  51. 51. ConclusionSo why is Node.js so popular?
  52. 52. ConclusionI think its becauseRyan Dhal put cool things together!!!
  53. 53. AGENDA• About Node.js• Programming in Node.js• Web Frameworks• Node.js Internals• Popularity of Node.js• Downside of Node.js
  54. 54. NODE.JS COMPLAINTS• Programmers are used to synchronous code• Exceptions are hard to handle• Difficult to debug• Most importantly it is not matured yet• Doesn’t utilize multi-core CPU
  55. 55. ConclusionFUTURE OF NODE.JS ???

×