Advertisement
Advertisement

More Related Content

Advertisement

Intro to Node.js (v1)

  1. Intro to Node.js Chris Cowan Lead Engineer http://www.plus3network.com
  2. What is Node.js? • Create by Ryan Dahl • Uses Chrome’s V8 Engine • Uses Non-Blocking I/O • Single Threaded • Server Side JavaScript • Active Community
  3. Node’s Goal is to provide an easy way to build scalable network programs.
  4. Node.js is NOT another web framework! But you can create a web framework using NPM modules.
  5. Node.js is… Web Server TCP Server Awesome Robot Controller Command Line Application Proxy Server Streaming Server VoiceMail Server Anything that has to deal with high I/O
  6. Node.js is Server Side JavaScript!
  7. Why Node.js? • Non Blocking I/O • Based on Chrome’s V8 Engines (FAST!) • 15,000+ Modules • Active Community (IRC, Mailing Lists, Twitter, Github) • Mac, Linux and Windows (all first class citizens) • One Language for Frontend and Backend • JavaScript is the Language of the Web
  8. Installing Node.js Mac OS X 1. Go to http://nodejs.org and click install 2. Install the downloaded package Windows 1. Go to http://nodejs.org and click install 2. Install the downloaded package Linux (and *nix variants) 1. Go to http://nodejs.org and click install 2. Decompress source and… ./configure … make … make install ( for Ubuntu use Chris Lea’s PPA – ppa:chris-lea/node.js )
  9. Some Basic Examples
  10. Hello World Create hello-world.js console.log(‘Hello World’); On the command line run node hello-world.js You should see Hello World
  11. Basic HTTP Server *Running this script my development box, I can achieve 10,000+ requests per second with 100 concurrent connections without breaking a sweat
  12. Some people use the core http module to build their web apps, most use a framework like Express or Connect or Flatiron or Tako or Derby or Geddy or Mojito or …
  13. Visit http://expressjs.com/guide.html for a detailed guide on using Express
  14. What is Non-Blocking I/O? And why should I care?
  15. Blocking I/ 270ms = SUM(user, activities, leaderboard)
  16. Non-Blocking I/ 150ms = MAX(user, activities, leaderboard)
  17. The most jarring thing about Server Side JavaScript is thinking in callbacks
  18. Callbacks are the Devil’s Work! Don’t go down this rabbit hole… One of the biggest mistakes is to get yourself in to callback hell by nesting callbacks inside of callbacks inside of more callbacks.
  19. Avoiding Callback Hell • Keep your code shallow • Break up your code into small chunks • Use a sequential library like async • Visit http://callbackhell.com
  20. Async to the rescue!
  21. Async provides several useful patterns for asynchronous control flow including: parallel, series, waterfall, auto and queue. Visit https://github.com/caolan/async for a detailed guide on using the async module.
  22. The Node Package Manager otherwise know as… NPM It’s how you harness the awesomeness of the Node.js community!
  23. Using NPM It’s standard practice to install modules locally for your current project. Modules are installed in the ./node_modules in the current directory. To Install a new module npm install <module> To find a module in the NPM repository npm search <search string> To list the modules (and their dependencies) in the current project npm list To see module details npm info <module>
  24. DON’T INSTALL MODULES GLOBALLY! Unless they are tools like node-dev, jake, express, minify-js OR linked development modules but more on that later.
  25. NPM is awesome sauce! Visit https://npmjs.org for more details about NPM and to browse the current NPM Repository
  26. Creating your own modules • Node.js uses CommonJS Modules • require(‘./example’) will load either example.js or example/index.js or the entry point specified in package.json • Run npm init to bootstrap your new module • Try to stick to creating Pure JavaScript modules if possible. It will give you less headaches down the road.
  27. Basic Module Example Everything exposed via module.exports is available as an instance variable. Once you’ve created a module you use it like this… Keep this in mind… modules are loaded once and cached. So when you load the module a second time in your app, require just returns the cache copied. This lets you do interesting things…
  28. Installing your module • Run npm link in the module working directory • Then run npm link <module> in the your project folder to link it from the global module to your local node_modules. • OR you can create a private registry (See https://npmjs.org/doc/registry.html) • OR just link it by hand :P
  29. My Favorite Modules • request • jake • async • hogan.js • node-dev • connect • underscore • moment • express • mysql
  30. Questions? Contact me at chris@chriscowan.us
Advertisement