• Like
Why Node.js
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
4,306
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
82
Comments
1
Likes
6

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

Transcript

  • 1. Why by 桂林 桂糊涂@weiboguilin1981@twitter
  • 2. About me2003, Delphi.2006, Java.2009, Python.2010, Node.js.Author of m on g osk in and st or m js
  • 3. What is node.js? Evented I/O for V8 JavaScript.
  • 4. What can node.js do?
  • 5. What can node.js do?Node.js as webserver
  • 6. What can node.js do?Node.js as webserverC10k problem
  • 7. What can node.js do?Node.js as webserverC10k problemComet
  • 8. What can node.js do?Node.js as webserverC10k problemCometWebsocket
  • 9. What can node.js do?Node.js as webserverC10k problemCometWebsocketNode.js as TCP server
  • 10. What can node.js do?Node.js as webserverC10k problemCometWebsocketNode.js as TCP serverNode.js as CLI tools
  • 11. What can node.js do?Node.js as webserverC10k problemCometWebsocketNode.js as TCP serverNode.js as CLI toolsNode.js as GUI tools
  • 12. Why node.jsWh y asy n c h r on ou s I O ?
  • 13. Why asynchronous IO?IO is the bottle-neck of application.
  • 14. Why asynchronous IO?IO is the bottle-neck of application. Network IO File IO Database IO
  • 15. Blocking socket IOclass ServerThread extends Thread { public ServerThread(socket){ this.socket = socket; } public void run(){ BufferedReader reader = newBufferedReader(this.socket.getInputStream()); String line; while((line = reader.readLine()) != null){ // block here // handle line } }}...ServerSocket serverSocket = new ServerSocket(port);while (true) { Socket s = serverSocket.accept(); // block here new ServerThread(s).start();}
  • 16. Non-blocking socket IOnet.createServer(function(socket){ socket.on(data, function(data){ // handle data }); socket.on(close, function(has_error){ // handle close }); socket.on(error, function(err){ // handle error });}).listen(port);
  • 17. Non-blocking IO vs Blocking IOnginx(non-blocking) vs apache(blocking)
  • 18. Node.js IO APIstreamfshttpnetdns
  • 19. Why node.jsWhy asynchronous IO?Wh y javasc r ip t ?
  • 20. Why javascript?
  • 21. Why javascript?
  • 22. Why javascript?
  • 23. Why javascript?Browsers war improving javascript.
  • 24. Why javascript?Browsers war improving javascript.Javascript is one of the f ast est dynamic programming language.
  • 25. Why javascript?Browsers war improving javascript.Javascript is one of the f ast est dynamic programming language.Javascript is one of the m ost p op u lar programming language.
  • 26. Why javascript?Browsers war improving javascript.Javascript is one of the f ast est dynamic programming language.Javascript is one of the m ost p op u lar programming language.Javascript has c losu r e (anonymoused function).
  • 27. Why javascript?Browsers war improving javascript.Javascript is one of the f ast est dynamic programming language.Javascript is one of the m ost p op u lar programming language.Javascript has c losu r e (anonymoused function).Javascript is cross-platform.
  • 28. Why node.jsWhy asynchronous IO?Why javascript?Wh y v8 ?
  • 29. About v8
  • 30. About v8V8 javascript VM is used in Goog le Ch r om e .
  • 31. About v8V8 javascript VM is used in Goog le Ch r om e .V8 team is led by L ar s B ak , one of the leading VM engineers in theworld with 20 years of experience in building VM.
  • 32. About v8V8 javascript VM is used in Goog le Ch r om e .V8 team is led by L ar s B ak , one of the leading VM engineers in theworld with 20 years of experience in building VM.L ar s B ak was the t ec h n ic al lead behind HotSpot(Sun’s Java VM).HotSpot improved Java’s performance 2 0 x t im es .Before HotSpot, L ar s B ak worked on a sm allt alk VM .
  • 33. How fast v8 is?Fast Property AccessDynamic Machine Code GenerationEfficient Garbage Collection
  • 34. V8 exampleThe JavaScript code to access property x from a Point object is:point.x
  • 35. V8 exampleThe JavaScript code to access property x from a Point object is:point.xIn V8, the machine code generated for accessing x is:# ebx = the point objectcmp [ebx,<hidden class offset>],<cached hidden class>jne <inline cache miss>mov eax,[ebx, <cached x offset>]
  • 36. Why node.jsWhy asynchronous IO?Why javascript?Why v8?Wh y t h r ead less?
  • 37. Why threadless?
  • 38. Why threadless?2mb stack per thread
  • 39. Why threadless?2mb stack per threadDead-locking
  • 40. Why threadless?2mb stack per threadDead-lockingThread-safe?
  • 41. Why threadless?2mb stack per threadDead-lockingThread-safe?Thread is design for blocking IO.
  • 42. Why node.jsWhy asynchronous IO?Why javascript?Why v8?Why threadless?Wh y n od e. js sp ec ial?
  • 43. Why node.js special?Pythoner:
  • 44. Why node.js special?Pythoner: Python is one of the most popular dynamic language too.
  • 45. Why node.js special?Pythoner: Python is one of the most popular dynamic language too. Performance of python is OK.
  • 46. Why node.js special?Pythoner: Python is one of the most popular dynamic language too. Performance of python is OK. We have good non-blocking web framework.
  • 47. Tornado by facebookTornado is a non-blocking web server, open-source by facebook.
  • 48. Hello Tornadoimport tornado.ioloopimport tornado.webclass MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world")application = tornado.web.Application([ (r"/", MainHandler),])if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
  • 49. Hello Nodejsvar http = require(http);http.createServer(function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Node.jsn);}).listen(3000, "127.0.0.1");
  • 50. Tornado with IOimport pymongoimport tornado.webclass Handler(tornado.web.RequestHandler): @property def db(self): if not hasattr(self, _db): # block here self._db = pymongo.Connection( host=127.0.0.1, port=27017).test return self._db def get(self): try: # block here user = self.db.users.find_one({username:self.current_user}) self.render(template, full_name=user[full_name]) except: raise tornado.web.HTTPError(500)
  • 51. Tornado with async IOimport asyncmongoimport tornado.webclass Handler(tornado.web.RequestHandler): @property def db(self): if not hasattr(self, _db): self._db = asyncmongo.Client(pool_id=mydb, host=127.0.0.1, port=27017, maxcached=10, maxconnections=50, dbname=test) return self._db @tornado.web.asynchronous def get(self): self.db.users.find({username: self.current_user}, limit=1,callback=self._on_response) def _on_response(self, response, error): if error: raise tornado.web.HTTPError(500) self.render(template, full_name=respose[full_name])
  • 52. Node.js with IOAnonymoused function, is very important for Evented IOvar mongo = require(mongoskin), http = require(http), jade = require(jade), db = mongo.db(localhost/test);http.createServer(function (req, res) { res.writeHead(200, {Content-Type: text/html}); db.users.findOne({username: req.session.current_user},function(err, user){ jade.renderFile(template, {user: user}, function(err, html){ res.end(html); }); });}).listen(3000, "127.0.0.1");
  • 53. What make node.js special?
  • 54. What make node.js special?Javascript is special.
  • 55. What make node.js special?Javascript is special.Most of node.js modules are asynchronoused.
  • 56. What make node.js special?Javascript is special.Most of node.js modules are asynchronoused.It’s easy to write threadless module.
  • 57. What make node.js special?Javascript is special.Most of node.js modules are asynchronoused.It’s easy to write threadless module.Node.js community is very active.
  • 58. How many modules?2320 modules can beinstall via npm2011, June 07.
  • 59. Node.js communityWhat do you thinkabout nodecommunity?
  • 60. Arunoda:Its the freedom followed by tools like github and npm. And everythingis growing fast.
  • 61. Pau:People is more open minded than in other programming communities.There is people coming from ruby, python, c, haskell, java, erlang,php…
  • 62. Mikeal Rogers:People using node.js are building stuff. the project seeks to make thelives better of people building products.
  • 63. Mikeal Rogers:People using node.js are building stuff. the project seeks to make thelives better of people building products.My experience with Python and Ruby is that their primary reason forworking on the project is around with a new language and/or vm. thepeople who work on “core” don’t build products and probably neverwill.
  • 64. Mikeal Rogers:People using node.js are building stuff. the project seeks to make thelives better of people building products.My experience with Python and Ruby is that their primary reason forworking on the project is around with a new language and/or vm. thepeople who work on “core” don’t build products and probably neverwill.Node.js is not a language and it doesn’t write it’s own vm. it’s notattractive to people who don’t care about building stuff with it.
  • 65. 桂林:潮不等于装13
  • 66. 桂林:潮不等于装13 , node.js很酷,也很务实。
  • 67. Why node.js
  • 68. Why node.jsWhy asynchronous IO?Never blocking, cpu efficientWhy javascript?Right and popluar languageWhy v8?Extremely fast VMWhy threadless?Easy, memory efficientWhy node.js special?Active and creative community
  • 69. Thank you