Upload
lakemove
View
678
Download
3
Embed Size (px)
DESCRIPTION
introduction to node.js
Citation preview
A brief intro to nodejs
Jay LJLU from Evenex team
• Built on Chrome’s JavaScript runtime• Event-driven• Non-blocking IO• Easily building Fast, scalable network application
Built on Chrome’s JavaScript runtime
• Server side, V8 engine• Fast • ECMA 5 standards compliance• “api bits that aren’t considered safe to use in a
web setting but are built in to node’s V8 engine.”
• Choosing JS over C,Lua,Haskell,Ruby
Event-driven
• EventEmitter• Event loop in libev• Functional programming in JS– First-class functions– Closure
• Used across all core modules
Non-blocking IO 1/2
• “I/O is expensive”
Non-blocking IO 2/2
• Long polling (chatroom/whiteboard), socket.io• Streaming, (Byte)Buffer• Blocking vs non-blocking• Context switch is expensive• C10K problem• Share same architecture with nginx
Easily building fast, scalable network application 1/2
• Easy– “NodeJs is the new Java”– Fancy core library – No compile, fast iteration
Easily building fast, scalable network application 2/2
• One language rule all. MEAN Stack. • “web”=>“network”• TJ Holowaychuk
Engineering
• NPM• IDE• Deployment• Coding style• Testing• Debugging• When to use nodejs• Companies using NodeJs
Engineering NPM
• Node packaged modules– https://npmjs.org– Now part of core node– Easily manage dependencies– package.json
Engineering IDE
• WebStorm– Commercial license – Truly IDE , good for beginner
• Sublime text– Free to use– Syntax highlight– Code template/completion– Node plugin
Engineering deployment
• Single thread, single process– Utilize my 8 cores CPU ? (new cluster module)
• Integrate with legacy services, not replace– Consumes JSON RESTful services
• Run behind Nginx– Not stable enough to be a web server
• Hosting
Engineering coding style
• Callback hell – Async.js to rescue
• err as first argument– Handle error first !
• Extending prototype– Do not extend the prototypes of any objects, especially
native ones
Engineering Testing
• Mocha – Fancy reports– BDD, TDD– Timeout – good for asyc– Runs in browser as well
• CI : grunt+jenkins– Alt : Travis CI https://github.com/travis-ci
Engineering debugging
• Chrome developer tool– Eclipse based
• Node-inspector– Browser based
Engineeringwhen to use nodejs
• When to use Node.js:• Use Node.js to:• Build a (soft) real-time social app like Twitter or a chat app.• Build high-performance, high I/O, TCP apps like proxy servers, PaaS, databases, etc.• Build backend logging and processing apps.• Build great CLI apps similar to vmc-tool, and build tools such as ant or Make.• Add a RESTful API-based web server in front of an application server.• When NOT to use Node.js:• Node.js is not suitable for every application:• Mission-critical (hard) real-time apps like heart monitoring apps or those that are CPU-
intensive.• For simple CRUD apps that don’t have any real-time or high-performance needs,
Node.js does not provide much of an advantage over other languages.• Enterprise apps that might need some specific libraries for which there may not be a
Node.js library yet. (However, you could build a polyglot app that uses Java in conjunction to Node.js to help with libraries.)
Companies using NodeJs
• LinkedIn, mobile webapp• Ebay, data retrival gateway• Github, for downloads• Dow Jones & Company, for WJS social site• PayPal (moving every product & every site)
Refs and links• http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/• http://www.slideshare.net/the_undefined/nodejs-best-practices-10428790• http://www.slideshare.net/the_undefined/nodejs-a-practical-introduction-v2• http://www.slideshare.net/oscarrenalias/nodejs-for-architects-openslava-2013
Getting started !!
• git checkout https://github.com/joyent/node• ./configure && make && sudo make install• vi example.js• node example.js
Thanks !
• 刘立杰 Jay LJLU
More interesting…
• CoffeeScript• Twitter Bower• Yeoman/grunt• phantomjs