Getting Started with Node.js

Learn how to run Node.js on Engine Yard Cloud.

_Jake Luer is a Node.js developer and consultant focused on building the next generation of mobile and web applications. He is logicalparadox on GitHub and @jakeluer on Twitter. _

Node.js is a JavaScript platform built on Google Chrome’s v8 runtime. Node’s website states that it “uses an event-driven, non-blocking I/O model,” but what does that mean? As an example, in the case of a web server, when a request is made, it will set up an asynchronous listener for when the code has prepared a response. During that time, the application can process more incoming requests, read the file system or perform database actions, all without blocking I/O of the process thread. This makes Node.js perfect for distributed, real-time applications.

Node has grown beyond buzz in recent months to become the go-to technology for rapid and highly scalable web application deployment. It is currently the second most-watched open source project on GitHub, and the number of developers releasing open source Node modules has grown five-fold in the past year. Startups are also flocking to Node as the technology of choice to quickly make their dream applications a reality.

Node.js is also gaining traction in the enterprise. LinkedIn has been vocal about using Node to power the server-side stack of the company’s mobile application. LinkedIn went from running 15 servers with 15 instances on each physical machine to just 4 instances that can handle twice the traffic.

eBay has also adopted Node for ql.io, a data-retrieval and aggregation gateway. Benchmarks by eBay indicate that the company can support 120,000 active connections per Node process on a standard workstation.

Modules, Dependencies and Collaboration: The Node Way

Node’s module system was built from the ground up to provide the cleanest possible experience for team collaboration and application deployment. Furthermore, many Node module developers have fully embraced the single responsibility principle to ensure a DRY codebase and high test coverage.

The Node Package Manager (NPM) is an online repository of open source modules that can be used as the basis for any Node application. By using the NPM command line utility included with Node, developers can easily publish their own modules or install and upgrade project dependencies. Node’s implementation of local, recursive dependency resolution ensures that each application maintains its own dependency tree.

Each Node module includes a package.json file which specifies that module’s meta data, including its dependencies. Node will recursively select the best-fit version of a dependency based on that module’s specification. Each module being used within the scope of a larger application will function exactly as the original developer intended.

The Node ecosystem is evolving rapidly, and modules are being updated quite frequently. While there is much to be said for staying “bleeding edge,” when deploying mission critical applications or collaborating in teams, a reasonable expectation is that every installation be identical. Rather than leave this to chance or checking dependencies into version control, NPM includes a command called shrinkwrap to recursively lock all dependency versions.

The NPM repository is at 12,000 modules and growing daily. To read more about NPM and browse available packages, please visit the NPM website.

Notable Modules

Though you can easily create a basic web server in Node with about four lines of code, the final section of this article will cover notable modules that can help you get your application up and running quickly.

Server Framework

Though there are many different frameworks for getting a Node web application started, Express by TJ Holowaychuk is by far the most prolific. Originally inspired by Ruby’s Sinatra framework, Express is a middleware-based asynchronous web application module. It includes a number of middleware components such as a router, session/cookie handlers, form and query string parsing and template rendering. It can also be easily extended with custom middleware to accommodate whatever your need may be.

  • Express: MIT Licensed, GitHub: visionmedia/express

    Realtime Framework

    As previously mentioned, Node.js is perfect for real-time web apps. If you are looking to provide a robust real-time experience for your application then Socket.io by LearnBoost needs to be in your tool belt. Socket.io abstracts and expands on the HTML5 webSocket protocol by providing an API that can be used in any browser. For browsers or mobile devices that do not provide HTML5 WebSockets, Socket.io will gracefully fall back on to another transfer protocol (such as long-polling) to provide the same, real-time experience to all of your users. Socket.io also enhances the WebSocket experience with multiplexing, scaling strategies and broadcasting.

  • Socket.io: MIT Licensed, GitHub: LearnBoost/socket.io

    Testing

    Once you have written your first application, you will need a way to keep everything running smoothly. There are many JavaScript test runners available but Mocha is the most versatile and easiest to learn. It provides both BDD and TDD interface styles and can easily be integrated with continuous integration tools; it can also be used to test browser features. Mocha, however, does not come with an assertion library: for that you should check out one of my own modules - Chai. It provides several different assertion styles, an expansive set of core assertions, and can be extended with plugins. Chai also works for both Node.js and the browser.

  • Mocha: MIT Licensed, GitHub: visionmedia/mocha
  • Chai: MIT Licensed, GitHub: chaijs/chai