The MEAN stack is NICE!

I’d been wanting to delve into Node.js for quite awhile, and over the past few months have finally gotten the chance; I am working on a re-build of my FreeHandicapTracker application using the MEAN stack. I have been pleasantly surprised at how easy everything has been, and how few pitfalls or time-sinks I have run into.

MEAN is an acronym for MongoDB, Express.js, Angular, and Node.js. It represents a totally different approach from the more traditional LAMP or Microsoft stacks. The common thread that ties each of these components together is the javascript language – so for developers who are excited about using a unified syntax across application layers, and enjoy writing javascript, this is a nice solution.

Here’s a brief explanation of each component:

MongoDB

Mongo is a free NoSQL Engine that will replace MySQL / Oracle / SQL Server as your primary data storage engine.

MongoDB is not a traditional Relational Database Management System (RDBMS). You won’t be working with a series of tables that have parent / child relationships where rows are related through foreign keys. Instead, the NoSQL approach features the concept of “collections”, where each collection in a database is comprised of a number of documents that are structured using a defined schema. MongoDB document schemas use a syntax that closely resembles JSON.

The JSON syntax of a Mongo document should appeal to a lot of developers who are comfortable working with javascript. It presents a lot of unique opportunities as well, especially in terms of flexible data structures within a single collection. For example, a document can have sub-documents built-in:

{
    _id : 3122,
    name : "Joe Johnson",
    address : {
        street_1 : "1922 Lois Ln",
        street_2 : "apt. 4",
        city : "Peoria",
        state : "IL",
        zip : 63453
    },
    phone_numbers : [
        "555-854-2558",
        "520-855-7878"
    ]
}

I’ve found there is a slight learning-curve involved with getting comfortable querying against sub-documents, etc – but for the most part it hasn’t been too bad. All major server-side languages now have libraries for interfacing with Mongo, and running ORM-style operations from an Express.js / Node environment has been particularly nice (see Mongoose and likely many others).

A major concept I have picked up from using Mongo in different back-end environments is that the emphasis should be on performance, sometimes at the expense of perfectly efficient schema design. In other words, redundant data across collections is acceptable if it will prevent subsequent queries within your application layer. Joining collections is not necessarily easy or advisable when using Mongo – if possible, the data you need in a given request should be available in the collection you are working with at the time.

Node.js

When I first heard about Node.js a few years back, I wasn’t sure what it was; in fact I assumed it was another javascript framework like Angular or Ember. Many Javascript frameworks have sprung up over the years – some have gained steam and sustained themselves while others have come and gone. It wasn’t until the popularity of Node.js had grown to nearly the point it is now, that I decided I had to look into it and discovered it is something different entirely.

In fact, Node.js is a javascript runtime environment that can be run on a server (pretty much any Operating System) and receive http requests. As such, it becomes a replacement for Apache or Windows web server software. It also replaces server-side languages like PHP, C#, or Java (you will write server-side operations in javascript). Node.js javascript tools and utilities for interfacing with your server’s filesystem and databases are built-in or readily available.

In my mind, Node.js really shines when it comes to dependency management and portability of applications. There is a package-manager utility and adjoining community known as NPM that is extremely robust at this point. Pretty much anything you’re going to need to do; from parsing JSON, to Validation, to connecting to a MySQL database, to Authorization frameworks are available for instant download as node extensions. Also, a convenient package.json file is generated that allows your project to be installed on different environments along with all of its dependencies using a single command (similar to composer for PHP applications).

What Node.js lacks out-of-the-box is viable structure for applications. This is where Express.js comes into play – read on!

Express.js

Express has emerged as the standard framework for Node.js applications; API layers or web apps. It provides a system for routing, code encapsulation, client-side templating, and error-handling / debugging.

Read more about how to install and use here: http://expressjs.com/

Angular.js

Angular is a javascript framework for client-side web applications, developed by Google. I have pretty limited experience with Angular as of yet, but from what I have seen / heard it allows for very modular development of web UI, and gives each component of your application specific scope. It also seems to take templating to the next level by automatically binding your page elements to their corresponding javascript models.

Definitely worth diving into – Angular V.2 has been recently introduced

Summary

While all of these components can be used independently, there is definitely a noticeable synergy that comes with packaging them together for your applications. In particular, I have found that using Node.js with Express and MongoDB can get you up and running with an API layer for your app, that can then be utilized from any web or device interface that you choose. I’ve had a lot of success with this development model recently with my golf app – will post specifics from that project with some examples soon.