A brief intro to nodejs

21
A brief intro to nodejs Jay LJLU from Evenex team

description

introduction to node.js

Transcript of A brief intro to nodejs

Page 1: A brief intro to nodejs

A brief intro to nodejs

Jay LJLU from Evenex team

Page 2: A brief intro to nodejs

• Built on Chrome’s JavaScript runtime• Event-driven• Non-blocking IO• Easily building Fast, scalable network application

Page 3: A brief intro to nodejs

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

Page 4: A brief intro to nodejs

Event-driven

• EventEmitter• Event loop in libev• Functional programming in JS– First-class functions– Closure

• Used across all core modules

Page 5: A brief intro to nodejs

Non-blocking IO 1/2

• “I/O is expensive”

Page 6: A brief intro to nodejs

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

Page 7: A brief intro to nodejs

Easily building fast, scalable network application 1/2

• Easy– “NodeJs is the new Java”– Fancy core library – No compile, fast iteration

Page 8: A brief intro to nodejs

Easily building fast, scalable network application 2/2

• One language rule all. MEAN Stack. • “web”=>“network”• TJ Holowaychuk

Page 9: A brief intro to nodejs

Engineering

• NPM• IDE• Deployment• Coding style• Testing• Debugging• When to use nodejs• Companies using NodeJs

Page 10: A brief intro to nodejs

Engineering NPM

• Node packaged modules– https://npmjs.org– Now part of core node– Easily manage dependencies– package.json

Page 11: A brief intro to nodejs

Engineering IDE

• WebStorm– Commercial license – Truly IDE , good for beginner

• Sublime text– Free to use– Syntax highlight– Code template/completion– Node plugin

Page 12: A brief intro to nodejs

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

Page 13: A brief intro to nodejs

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

Page 14: A brief intro to nodejs

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

Page 15: A brief intro to nodejs

Engineering debugging

• Chrome developer tool– Eclipse based

• Node-inspector– Browser based

Page 16: A brief intro to nodejs

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.)

Page 17: A brief intro to nodejs

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)

Page 18: A brief intro to nodejs

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

Page 19: A brief intro to nodejs

Getting started !!

• git checkout https://github.com/joyent/node• ./configure && make && sudo make install• vi example.js• node example.js

Page 20: A brief intro to nodejs

Thanks !

• 刘立杰 Jay LJLU

Page 21: A brief intro to nodejs

More interesting…

• CoffeeScript• Twitter Bower• Yeoman/grunt• phantomjs