For the past few months I’ve been seeing more and more about node.js popping up all over the place.  I installed it and dabbled a few months ago, but never really spent more than an hour and honestly didn’t walk away very impressed (my own fault, really).  This past week I decided to really give it another shot, really trying to dig into it and actually accomplish something.  This also sparked by my discovery of Koding.com and Cloud9 and their exceptional support for node.js development.

There are a lot of technical reasons why node.js is becoming so popular (versus any other back-end technology) that I won’t go into (because honestly I couldn’t really explain it) and while it may or may not be right for you – there’s no denying that it’s quickly gaining ground and probably not going away!

First, I started reading a book on building web apps with Backbone and node.js – but the coverage of node.js was very cursory and didn’t give me nearly enough information to be comfortable.  Then I watched a course on Pluralsight specifically about programming with node.js.  Again, this was good, but it was pretty raw and hardly mentioned web development.  Towards the end of the course, however, it mentioned another course that specifically covered ExpressJS – a framework built on node.js specifically for web development.

After completing this course (roughly 3 hours) I felt enlightened!  I couldn’t help smiling the whole time I watched the course because everything was just making sense and it was all so clear and logical.  It helps that the author clearly has experience with .net development as he constantly makes references to similar concepts in .net.

The beauty was that using what I already know with .net MVC development, I immediately felt comfortable with the ideas behind ExpressJS.  On top of that, my love for JavaScript made coding in the environment even more enjoyable!  And again, I can’t stress how much I am loving the Cloud9 service/IDE!  (Although, I can definitely see the need to use something with a bit more muscle like WebStorm when doing serious node.js development so you can more easily debug during run-time ,etc.)

A (super) basic ExpressJS web project explained:

[alert style=”alert-info”]If you like, you can look at this project to follow along with the ideas/concepts I discuss in the section below:

https://c9.io/jkat98/ugm2013

Also note how awesome the Cloud9 IDE is while you check out the project ;)[/alert]

Personally I am a fan of the MVC pattern of web development.  I really do love what Microsoft has done with .net MVC (and to be fair, what everyone else has already been doing for a long time!).  You can very easily apply an MVC pattern to a web application using ExpressJS and node.js!

The project is organized as follows:

  • app.js – the root of the application.
    The core ExpressJS application object is booted up, along with server dependencies and any middleware support/configuration.  Additionally, the main connection to my db is here, as well as all of the routes for the application (think of this like the global.asax file in a .net web app).  However, since I didn’t want my app.js file getting out of hand, I moved my routes to an external object.  node uses CommonJS which is like require.js – which allows you to “include” modules into each file (much like a “using” in .net development).  Therefore, since I decided to pull my routes to an external file, I needed to be sure to include it by referencing var routes = require(“./app/routes”).
    The server is started by simply calling (via a terminal command line) “node app.js”

  • /app/routes.js – contains all of the routes (paths) to the application as well as pointers to a controller for each.
    The routes file simply contains a list of gets and posts along with the actual route path and corresponding controller function.  So, for example, “/” (or the root of the site) points to my home (index.js) controller document, specifically the index function.  A post to the homepage (using the form thats right on the homepage) uses the home controller’s register function (which handles the submission and inserts a record into the db).

  • /controllers/ – contains a file for each route, with however many functions to handle the different patterns. This matches pretty closely with how a .net MVC project is organized as well (except .net MVC controllers are actually named “UsersController.cs” where I just named mine “users.js” – this is purely personal preference whereas in .net MVC its a rule of convention.

  • /data/ – used by mongoDB to store all of the data documents – mongoDB is a nosql so the data is stored in BSON (binary JSON) files unlike typical MS SQL databases. (You don’t ever touch this folder, is is purely for file storage used by mongo.)

  • /models/ – simple data model definitions (using mongoose – which is a framework to make working with mongodb easier in node)  Again, very much like how view models work in .net MVC.

  • /node_modules/ – this is the default folder that all of your packages are stored in.  This is like the References folder in a .net project. (Node’s NPM is just like .net’s NuGet)

  • /public/ – this is similar to the Content folder in a .net MVC project.  This is where all of your client files will be stored including css files, images, javascripts for the client, etc.
    Using middleware with ExpressJS, you can easily integrate support for LESS/SASS in your CSS files!

  • /views/ – this is exactly like the Views folder in a .net MVC project.
    ExpressJS, by default, uses the Jade view engine (which again, is comparable to .net MVC’s Razor).  Jade is very interesting – as its sort of like Zen Coding, but without the real-time conversion component that most editors support.  You actually just code your HTML docs using a Zen like style.  Its very weird at first, but once you get used to it it starts to make sense and actually makes flowing HTML docs much quicker!

If you are familiar with Backbone development, then developing with node and Express will come VERY naturally to you.  A lot of the concepts are the same/similar and obviously its all JavaScript based!

If you are at all interested in learning a new backend technology, and have a pretty good understanding of JavaScript I strongly urge you to check out a few tutorial videos or books on node.js development (specifically using ExpressJS) and get started today!