Why Nodejs Guilin Shanghai
-
Upload
jacksontian -
Category
Technology
-
view
2.042 -
download
1
description
Transcript of Why Nodejs Guilin Shanghai
![Page 1: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/1.jpg)
Why
by 桂林
桂糊涂@weibo
guilin1981@twitter
![Page 2: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/2.jpg)
About meAbout me
2003, Delphi.
2006, Java.
2009, Python.
2010, Node.js.
Author of mongoskinmongoskin and stormjsstormjs
![Page 3: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/3.jpg)
What is node.js?What is node.js?
Evented I/Ofor
V8 JavaScript.
![Page 4: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/4.jpg)
What can node.js do?What can node.js do?
![Page 5: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/5.jpg)
What can node.js do?What can node.js do?
Node.js as webserver
![Page 6: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/6.jpg)
What can node.js do?What can node.js do?
Node.js as webserverC10k problem
![Page 7: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/7.jpg)
What can node.js do?What can node.js do?
Node.js as webserverC10k problem
Comet
![Page 8: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/8.jpg)
What can node.js do?What can node.js do?
Node.js as webserverC10k problem
Comet
Websocket
![Page 9: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/9.jpg)
What can node.js do?What can node.js do?
Node.js as webserverC10k problem
Comet
Websocket
Node.js as TCP server
![Page 10: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/10.jpg)
What can node.js do?What can node.js do?
Node.js as webserverC10k problem
Comet
Websocket
Node.js as TCP server
Node.js as CLI tools
![Page 11: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/11.jpg)
What can node.js do?What can node.js do?
Node.js as webserverC10k problem
Comet
Websocket
Node.js as TCP server
Node.js as CLI tools
Node.js as GUI tools
![Page 12: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/12.jpg)
Why node.jsWhy node.js
Why asynchronous IO?Why asynchronous IO?
![Page 13: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/13.jpg)
Why asynchronous IO?Why asynchronous IO?IO is the bottle-neck of application.
![Page 14: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/14.jpg)
Why asynchronous IO?Why asynchronous IO?IO is the bottle-neck of application.
Network IO
File IO
Database IO
![Page 15: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/15.jpg)
Blocking socket IOBlocking socket IOclass ServerThread extends Thread { public ServerThread(socket){ this.socket = socket; } public void run(){ BufferedReader reader = new BufferedReader(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();}
![Page 16: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/16.jpg)
Non-blocking socket IONon-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);
![Page 17: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/17.jpg)
Non-blocking IO vs Blocking IONon-blocking IO vs Blocking IO
nginx(non-blocking) vs apache(blocking)
![Page 18: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/18.jpg)
Node.js IO APINode.js IO API
stream
fs
http
net
dns
![Page 19: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/19.jpg)
Why node.jsWhy node.js
Why asynchronous IO?
Why javascr ipt?Why javascr ipt?
![Page 20: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/20.jpg)
Why javascript?Why javascript?
![Page 21: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/21.jpg)
Why javascript?Why javascript?
![Page 22: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/22.jpg)
Why javascript?Why javascript?
![Page 23: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/23.jpg)
Why javascript?Why javascript?
Browsers war improving javascript.
![Page 24: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/24.jpg)
Why javascript?Why javascript?
Browsers war improving javascript.
Javascript is one of the fastestfastest dynamic programming language.
![Page 25: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/25.jpg)
Why javascript?Why javascript?
Browsers war improving javascript.
Javascript is one of the fastestfastest dynamic programming language.
Javascript is one of the most popularmost popular programming language.
![Page 26: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/26.jpg)
Why javascript?Why javascript?
Browsers war improving javascript.
Javascript is one of the fastestfastest dynamic programming language.
Javascript is one of the most popularmost popular programming language.
Javascript has c losureclosure(anonymoused function).
![Page 27: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/27.jpg)
Why javascript?Why javascript?
Browsers war improving javascript.
Javascript is one of the fastestfastest dynamic programming language.
Javascript is one of the most popularmost popular programming language.
Javascript has c losureclosure(anonymoused function).
Javascript is cross-platform.
![Page 28: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/28.jpg)
Why node.jsWhy node.js
Why asynchronous IO?
Why javascript?
Why v8?Why v8?
![Page 29: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/29.jpg)
About v8About v8
![Page 30: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/30.jpg)
About v8About v8
V8 javascript VM is used in Google ChromeGoogle Chrome.
![Page 31: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/31.jpg)
About v8About v8
V8 javascript VM is used in Google ChromeGoogle Chrome.
V8 team is led by Lars BakLars Bak , one of the leading VM engineers in theworld with 20 years of experience in building VM.
![Page 32: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/32.jpg)
About v8About v8
V8 javascript VM is used in Google ChromeGoogle Chrome.
V8 team is led by Lars BakLars Bak , one of the leading VM engineers in theworld with 20 years of experience in building VM.
Lars BakLars Bak was the technical leadtechnical lead behind HotSpot(Sun’s Java VM).HotSpot improved Java’s performance 20x t imes20x t imes.
Before HotSpot, Lars BakLars Bak worked on a smalltalk VMsmalltalk VM .
![Page 33: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/33.jpg)
How fast v8 is?How fast v8 is?
Fast Property Access
Dynamic Machine Code Generation
Efficient Garbage Collection
![Page 34: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/34.jpg)
V8 exampleV8 exampleThe JavaScript code to access property x from a Point object is:point.x
![Page 35: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/35.jpg)
V8 exampleV8 exampleThe JavaScript code to access property x from a Point object is:point.x
In 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>]
![Page 36: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/36.jpg)
Why node.jsWhy node.js
Why asynchronous IO?
Why javascript?
Why v8?
Why threadless?Why threadless?
![Page 37: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/37.jpg)
Why threadless?Why threadless?
![Page 38: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/38.jpg)
Why threadless?Why threadless?
2mb stack per thread
![Page 39: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/39.jpg)
Why threadless?Why threadless?
2mb stack per thread
Dead-locking
![Page 40: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/40.jpg)
Why threadless?Why threadless?
2mb stack per thread
Dead-locking
Thread-safe?
![Page 41: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/41.jpg)
Why threadless?Why threadless?
2mb stack per thread
Dead-locking
Thread-safe?
Thread is design for blocking IO.
![Page 42: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/42.jpg)
Why node.jsWhy node.js
Why asynchronous IO?
Why javascript?
Why v8?
Why threadless?
Why node. js special?Why node. js special?
![Page 43: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/43.jpg)
Why node.js special?Why node.js special?Pythoner:
![Page 44: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/44.jpg)
Why node.js special?Why node.js special?Pythoner:
Python is one of the most popular dynamic language too.
![Page 45: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/45.jpg)
Why node.js special?Why node.js special?Pythoner:
Python is one of the most popular dynamic language too.
Performance of python is OK.
![Page 46: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/46.jpg)
Why node.js special?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.
![Page 47: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/47.jpg)
Tornado by facebookTornado by facebook
Tornado is a non-blocking web server, open-source by facebook.
![Page 48: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/48.jpg)
Hello TornadoHello Tornadoimport tornado.ioloopimport tornado.web
class 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()
![Page 49: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/49.jpg)
Hello NodejsHello Nodejsvar http = require('http');http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Node.js\n');}).listen(3000, "127.0.0.1");
![Page 50: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/50.jpg)
Tornado with IOTornado with IOimport pymongoimport tornado.web
class 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)
![Page 51: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/51.jpg)
Tornado with async IOTornado with async IOimport asyncmongoimport tornado.web
class 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'])
![Page 52: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/52.jpg)
Node.js with IONode.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");
![Page 53: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/53.jpg)
What make node.js special?What make node.js special?
![Page 54: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/54.jpg)
What make node.js special?What make node.js special?
Javascript is special.
![Page 55: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/55.jpg)
What make node.js special?What make node.js special?
Javascript is special.
Most of node.js modules are asynchronoused.
![Page 56: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/56.jpg)
What make node.js special?What make node.js special?
Javascript is special.
Most of node.js modules are asynchronoused.
It’s easy to write threadless module.
![Page 57: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/57.jpg)
What make node.js special?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.
![Page 58: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/58.jpg)
How many modules?How many modules?
2320 modules can beinstall via npm2011, June 07.
![Page 59: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/59.jpg)
Node.js communityNode.js community
What do you thinkabout nodecommunity?
![Page 60: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/60.jpg)
Arunoda:Arunoda:
Its the freedom followed by tools like github and npm. And everythingis growing fast.
![Page 61: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/61.jpg)
Pau:Pau:
People is more open minded than in other programming communities.There is people coming from ruby, python, c, haskell, java, erlang,php…
![Page 62: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/62.jpg)
Mikeal Rogers:Mikeal Rogers:
People using node.js are building stuff. the project seeks to make thelives better of people building products.
![Page 63: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/63.jpg)
Mikeal Rogers: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.
![Page 64: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/64.jpg)
Mikeal Rogers: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.
![Page 65: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/65.jpg)
桂林:桂林:
潮不等于装13
![Page 66: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/66.jpg)
桂林:桂林:
潮不等于装13 , node.js很酷,也很务实。
![Page 67: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/67.jpg)
Why node.jsWhy node.js
![Page 68: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/68.jpg)
Why node.jsWhy node.js
Why asynchronous IO?Never blocking, cpu efficient
Why javascript?Right and popluar language
Why v8?Extremely fast VM
Why threadless?Easy, memory efficient
Why node.js special?Active and creative community
![Page 69: Why Nodejs Guilin Shanghai](https://reader034.fdocuments.in/reader034/viewer/2022050801/545aed67b1af9f4a1d8b4b88/html5/thumbnails/69.jpg)
Thank youThank you