If you’re like me, you’ve wanted to check out Node, also called Node.js, for awhile. While Orchestrate customers use many different technologies, we’ve noticed our Node library is particularly popular. I expect Node developers like Orchestrate for similar reasons they like Node: both are lightweight, have low barriers to entry, and are actually kind of fun. In this post, I’ll take a look at a handful of Node frameworks.

As I started to look into using Node for a project, I realized I’d need a framework if I wanted to build a web application. Node’s flexibility means it can also be a mobile backend, a pipe for real-time data or an API. However, that means it does not come with a concept of views and routes, two things needed for building a web application. I dug into three popular frameworks that provide those two features and a whole lot more.

Keep It Simple, Stay Hapi

Hapi.js is a basic framework that was created by Walmart Labs, a division of the giant retailer that is known for its early adoption of Node. Hapi aims to “enable developers to focus on writing reusable application logic instead of spending time building infrastructure,” according to the project’s home page.

For my uses, I just need a simple JavaScript file to declare my routes, and a templates directory to hold my HTML. The step-by-step getting started guide helped me learn one concept at a time. I did get a little lost moving from a simple server to one built on templates.

Here’s what my JavaScript server file looks like:

var Path = require('path');
var Hapi = require('hapi');

var serverOptions = {
    views: {
        engines: {
            html: require('handlebars')
        },
        path: Path.join(__dirname, 'templates')
    }
};
var server = new Hapi.Server(3000, serverOptions);

server.route({
    method: 'GET',
    path: '/adam',
    handler: function (request, reply) {
        reply.view('main', { title: 'Special route for Adam' });
    }
});

server.start(function () {
    console.log('Server running at:', server.info.uri);
});

I declared templates for my views to be stored in the templates directory. I used the Handlebars template engine, because that’s what Hapi showed in its examples. A number of other engines are available if that’s your style. The key bits are the data that gets passed along to reply.view—the first is the name of the template (without the file extension) and the second is the data that will be inserted into curly-braced {{template_fields}}.

Express Means Fast

Express is the best known framework of the three, and we use it at Orchestrate. Its getting started guide takes you through some basic commands, but when I noticed the Express generator, I jumped ahead to that. It’s a Node module itself, installed separately.

When you run Express generator, it creates a skeleton of an application. The directory structure, template and basic routing code is all set up automatically. Essentially, the generator gives you a running hello world app in one command.

There’s a lot of boilerplate that comes with the application setup, but for my needs I found a few key areas:

  • The routes directory contains individual files for routing traffic to your application.
  • The views directory has templates, which are by default Jade files.
  • The app.js file contains the heart of your application setup, most notably pointing to your routes and views.

It appears that Express is very modular, with many different files to touch. One of the major upsides of Express, it seems, is this ability to piece together other technologies. So, it’s not necessarily a downside that the files you’ll need are in different places, but it can make it a little slower going until you get used to it.

Set Sails for Node on Rails

Sails is a Node framework with an approach that will be familiar to Ruby on Rails developers. It fully embraces the Model View Controller (MVC) pattern, with scaffolding features built into the framework. By comparison, Express requires a plugin for scaffolding.

Sails has a generator built in to stub out new applications. You can also add models from the command line interface and quickly start adding some data to your project via the API that comes ready-to-go in every Sails project. Even though it’s the fullest-featured of the three frameworks, Sails is nevertheless extensible. There are three different concepts for extending Sails: generators, adapters and hooks.

That’s all a bit more than is needed for a simple web application. Remember, I only want a way to route traffic and show pages. It can be a lot to wade through, so look for these files/directories:

  • The views directory has templates, which are by default Embedded JavaScript
  • The config/routes.js file determines routing to a view (for static content) or a controller
  • The api/controllers directory contains controllers, which can be stubbed out with the command line interface

Of the three frameworks, this one might take the most for you to wrap your head around, especially if you’ve never used MVC before. However, once you get used to it, you may find this framework and its embrace of the MVC pattern very powerful.

These are just three of many frameworks available. As I tinkered with each, the similarities between them helped me get a better feel for Node. For this reason, I recommend trying all three as an educational experience.

If you’re looking for one to dive into for a project, consider:

  • Hapi for its simplicity and ease of getting started without additional cruft.
  • Express for its ubiquity and extensibility.
  • Sails for its organization and Rails-like MVC experience.

If it’s any help, Orchestrate’s dashboard is built upon Express. We also have many tutorials that show how to use our database APIs with Express, including this Node.js authentication application walk-through.