a travel to node.js: what?

André da Silva Carrilho

The destination is set and now we have to prepare for the journey. Where to start? What’s available? How to use it? These are all questions I had and this post is to try to shed some light on all of this.

Before we start with the ‘what is what’ I’d like to recommend that you read the original documentation of every tool presented here before searching elsewhere. Normally the original documentation has all the information one needs to get started. If I think there are other resources that complement the original documentation I’ll post here.

Node.js: it’s a javascript platform that runs on top of Chrome’s Javascript V8 runtime which makes it super fast. What makes it different is how it processes requests allowing one instance to process more requests efficiently compared to other webservers (IIS, Apache). Read through their documentation to learn more.

Node Package Manager (npm): as the name says, it manages node’s packages. It handles all the hassle of downloading, installing and loading packages and it’s dependencies. Although node comes with basic packages for http and io operations npm has a bunch of other cool stuff you’ll eventually need.

Express: it’s a web framework that runs on top of node. Although it’s super light, it comes with the necessary components to build your first “Hello World” web app.  As far as I know all other web app frameworks (meanio, compoundjs) are build on top of express.

Express View Engine/Template: Based on my research the most popular are Jade, Hoganjs (hjs), EJS but there are other templates out there that you can use. I liked Hogan because it was built by twitter (if you know Twitter Bootstrap you’ll know what I’m talking about) and it seemed performant. But it lacked documentation and because it used mustache on the background it conflicted with angular templating which I wanted to use on the frontend. Although I know I can change the delimiters I wanted as minimum configuration as possible. EJS has alot of spaguetti code which I tried to avoid from my ASP times. So I chose Jade because it’s Express default template, it seemed clean, had a bunch of cool features (partials, blocks, etc), great documentation and a lot of people use it. One drawback is that I was unable to install the textmate bundle which is a minor problem compared to the tons of features it has Found a new Textmate bundle that works great. Go through their documentation, it’s all there and it’s extremely simple to learn and use it.

nodemon: you can start your node app running:

node app

but whenever you make a change in your code like installing a new package you’ll have to stop and restart it. After a while it’s annoying and this is where nodemon come in. Whenever it detects something changed in your app it automatically restarts without you having to do anything. A useful timesaver.

Bower (Bower on GitHub): It’s a package manager similar to npm but for client side packages like angularjs, jquery, twitter bootstrap, etc. Read this for differences between bower and npm. The cool thing about this is that all 3rd party packages loaded with bower are stored on the same folder so you can separate your code from 3rd party packages.

Grunt: As the site states, it’s all about automation. Basically define automation tasks for minification, compilation, unit testing, etc. Once configured all this can be done by running the grunt command line utility without ever needing to worry about it. At first this can be a bit overwhelming because of all the configurations and setup but once it’s done you’ll be glad to have it.

Passport: it is an authentication middleware. The only reason I used it is because it has integration OAuth and OpenID and supports Facebook, Twitter and Google out-of-the-box which is helpful for certain kinds of apps. Even if you want to create your own authentication endpoint you can. Passport calls the different endpoint Strategy.

Knex & Bookshelf: Knex handles connecting, manage pooling and querying for different databases: PostgreSQL, MySQL, MariaDB and SQLite3. Bookshelf is an ORM that works side-by-side with Knex. Because of this, the same code should work with any of the databases that knex supports only modifying db (I haven’t tried it but I believe it should be the case). It is based on proven patterns supported by Backbone.js which is a plus.

Mongoose: The best way to work with MongoDB, a NoSQL database. I already worked with mongo with .NET but never did a trial run with nodejs + mongoose but from what I hear mongoose is the best tool for the job.

Bluebird (a promise library): Before you use bluebird you should understand the concept of promises. In a nutshell, using promises reduces nested code by combining into something like a fluent API. It also provides error handling bottom to top and also makes asynchronous code seem synchronous (in a non-blocking event-driven environment this is like turning rock into gold). For more detail you should read You’re Missing The Point Of Promises and Promise Nuggets. Promise anti patterns is also a good read for what not to do.

Checkit: a validation framework (if we can call it that) that validates any javascript object, be that server side (node app) or client side (browser). For example, a good way to validate user input.

Angular.js: From the likes of Backbone.js and Knockout.js angular is perfect for creating single page applications. With it’s bi-directional binding feature it takes ALOT of work out of our hands. And it is maintained by Google which is saying something! Definitely one of the best packages for building fully fledged client side applications.

Database: You probably already worked with some kind of database. It really doesn’t matter which one because from what I’ve seen node supports every single one, RDMS or NoSQL. Which database to use depends solely on your specific needs and how comfortable you are. I opted for PostgreSQL because I never worked with it (if I’m going to learn a new language might as well go all the way), it’s free which is a huge plus and, based on reviews by other developers, it has amazing features (video: Rob Conery – Five Things You Didn’t Know About PostgreSQL and, from the same author, this great article PostgreSQL Rising)

Hopefully this can shed some light on what you’ll find traveling the node path. Don’t be overwhelmed by all the information. I confess it’s alot to sink in but if you go step-by-step you’ll get the hang of it, I promise.

To make it easier I recommend you know what you want do before you try anything. Don’t try to read everything if you’re not gonna use it. Be sure were you want to go with your project, get the tools and choose the functionality to get there. In time you’ll learn all of it.

Have a nice trip to nodeland!