Search This Blog

Tech Book Face Off: Practical Node.js Vs. Node.js the Right Way

I've recently gotten into some server-side JavaScript development at work, and I needed to take a crash course in Node.js to make better progress, so I did what I always do. I picked up a couple of books on the subject and dug in. I did some perusing of reviews on Amazon.com and settled on a couple of books that appeared to be solid: Practical Node.js by Azat Mardan and Node.js the Right Way by Jim R. Wilson.

The funny thing about Node.js (I'll refer to it as Node from now on) is that there's not that much to talk about, so you quickly move on to talking about all of the libraries that you can plug into Node to get stuff done. That's because Node isn't a framework like Ruby on Rails or ASP.NET MVC. Node is a runtime that runs on the Chrome V8 engine, and all it does is allow you to run JavaScript directly on any machine outside of a browser. It includes a small, tight collection of APIs for working with file systems, operating systems, and networking interfaces, and it establishes an asynchronous paradigm for writing and executing code.

This asynchronous paradigm takes a little getting used to. Basically, any function call that could block, be it a file system access, database access, or HTTP request, will take a callback function as its last parameter and return immediately. When the slower operation finally finishes, the callback will be executed with the response from the operation as its parameters. If you forget about this behavior, you'll very quickly run into situations where you're trying to read things out of a database that haven't been written into it yet, or getting empty responses from HTTP requests. Basically, all dependent code needs to be executed inside callbacks, and that leads to the well-known Node existence of Callback Hell. We'll talk more about how to deal with that in a minute, but that pretty much is Node in a nutshell. Let's take a look at the books.

Practical Node.js front coverVS.Node.js the Right Way front cover

Practical Node.js


This book assumes you already know JavaScript, so Mardan does a brief run through of the basic JavaScript features you'll need to use Node while he explains how to install and use Node with the file system and networking interfaces. He also demonstrates the proverbial Hello, World! application in Node:
var http = require('http');
http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello, World!\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337');
This code creates an HTTP server that you can access from your browser at the address http://127.0.0.1:1337, and it will display the message "Hello, World!" That's chapter one, the only chapter directly about Node. The rest of the book is all about different npm modules that make it much easier to develop web applications. Npm is the Node package manager, and it's how you install and keep track of versions of all of the different Node packages you'll use.

The next chapters cover how to set up Express.js to create web apps that route requests to page-specific code and serve pages from HTML templates; how to do test-driven development and behavioral-driven development with Mocha.js; and how to use Jade and Handlebars to more easily create HTML templates for your web app. Through this primer, Mardan develops a simple blog website as a vehicle for showing what all of the code looks like and how it fits together. It's nice and accessible while you're getting comfortable with the asynchronous nature of the code, and you don't have to write tons of code to get visible results. Maybe it's not terribly original, but it's familiar and it works.

Mardan continues with the blog example while showing how to connect to a Mongo database with the bare bones Mongoskin driver, how to authorize and keep track of users with OAuth and Sessions, and how to leverage Mongo even more with the Mongoose ORM (Object Relational Mapping) library. The last handful of chapters cover topics about creating REST API servers, working with WebSockets, getting an app production ready, deploying an app, and publishing Node modules to contribute back to the open source community. After all, we are using an awful lot of great open source software here, and it would be nice to pay forward some of that kindness and hard work.

Overall, Practical Node.js was a good tour of some of the popular Node packages out there for developing web apps. It doesn't cover everything or go deep into high-scalability issues, but it definitely covers the essentials for getting started. Using the packages described in this book will give you a solid head start on your next Node application. 

Node.js the Right Way


Wilson takes a different tack than Mardan with Node.js the Right Way. He strips down both the example and the number of different packages that he covers, and ends up with a clear, concise Node introduction in less than half the pages of Practical Node.js. He starts out with a very quick intro of installing Node, working with the file system, and opening network sockets. Then he goes into how to implement a messaging service and build a scalable cluster of servers with the 0MQ messaging queue package.

The next topic is the database, and instead of Mongo, Wilson explains how to use CouchDB with Node. CouchDB is similar to Mongo in many ways, including that they are both JSON document based stores, but CouchDB scales differently than Mongo does. In the example Wilson uses, he has you download the Project Gutenberg dataset, a catalog of over 43,000 books, to experiment with. That's pretty cool, and a nice dataset to mess around with.

The last couple chapters cover Express.js for the web services, Passport for authentication, and Redis for ultrafast in-memory storage. Like Practical Node.js, this book spends most of its pages on topics surrounding Node, focusing on the packages that you would use with Node to create reliable, scalable web apps. Personally, I like the no-frills, straight-to-the-point way that Wilson presented most of the topics here so that I could quickly read the material, understand the most important pieces of building Node apps, and get back to solving the problems I was trying to solve. It was a nice little book for getting up and running with Node.

The First Rule of Node is: You Do Not Talk About Node


Both of these books spent the majority of their space on things other than Node itself. Once you understand the philosophy of Node, you don't need to keep talking about it. Node doesn't solve any problems. It enables you to solve problems quickly and efficiently. And there are tons of packages and modules out there to use with Node for solving your problems. Practial Node.js goes into a bit more depth on a wider variety of these Node packages while Node.js the Right Way hits hard and fast on a few essential topics. Each book covers slightly different packages, and it doesn't take too much time to get through them both so that's certainly one option. If you need a super quick primer, go with Node.js the Right Way. Otherwise, either or both books will do fine.

In the end you'll be spending most of your time in the package-specific documentation and solving your own application-specific problems, which is where your time is best spent anyway.

Oh, I almost forgot. How do you alleviate asynchronous Callback Hell? Use the async package, of course.

No comments:

Post a Comment