Monday, March 21, 2016

Understanding the MEAN stack - Part 2 : Express JS

In my previous blog we established that Node JS is actually not the web server but the platform on which you build your web server. Express JS is the actual web server module. Express JS is a Common JS module that you get into your application by requiring from Node JS. Traditionally it was the other way around. We would deploy our application into a web server, but here we bring in the web server capabilities into our application. 

Before we go further with the properties of Express JS let us understand what exactly does it mean to require in a Common JS module. 

Common JS modules standardize the way of working with Javascript outside the browser. Common JS is a specification more like a design pattern. There are a number of libraries/modules which are implemented using this specification e.g.: express and you can build your own modules too. Like I told in my previous blog it mainly addresses the single global namespace issue with javascript. Actually a Common JS module is nothing but a javascript file. Each module is written in a single javascript file and has an isolated scope that holds its own variables. In this scope there are 2 key components:
  • module.exports - This object is contained in each module and allows you to expose pieces of code when the module is loaded
//In myCommonJSModule.js
module.exports = foo;

function foo() {
    //... do something
}
  • require - It loads the module into your code
var myModule = require('./myCommonJSModule.js');
myModule.foo();

Ok now let us take a closer look at express. So in my previous blog I told that if you go and take a look at the example code in the about page of Node JS it shows how to create a simple HTTP web server. Clearly Node JS can directly use the http module to create a web server. Then why do we need express you ask? Lets take a look:

Express extends the core capabilities of http and along with that it brings 2 key functionalities:
  • Middleware
  • Routing
If you use the http module a lot of work like parsing the payload, selecting the right route pattern based on regular expression will have to be re-implemented.

Middleware: What is middleware? From the express.js site, "Middleware functions are the functions that have access to the request object, response object and the next middleware function in the application's request-response cycle.". Express provide middleware functions for things like logging the requests to console, parsing the url, parsing the body of the response, setting the location of the static files like css, js and html files.

Routing: Routing refers to the definition of endpoints (URIs) to an application and how it responds to client requests. A route is a combination of a URI, a HTTP request method (GET, POST etc) and one or more handlers for the endpoint.

So when a client sends a request to the path the callback executes where we connect to our next tier in our stack i.e. MongoDB.

So to conclude the key piece that express gives us is the capability to build the web server. But more importantly it gives us the restful end-points that is going to be called from our presentation tier (Angular JS) and once we get into the server methods it is going to reach out to the persistence tier (MongoDB). So in a way it acts like a bridge that gaps the tiers in our application.

Friday, March 18, 2016

Understanding the MEAN stack - Part 1 : Node JS

So a couple of weeks I was given this assignment to build a single page web application using the MEAN (MongoDB, Express JS, Angular JS, Node JS) stack as part of an interview. It was a simple todo application with features functionalities to add a todo item, mark a todo item as completed and delete a todo item. Given the fact that I have very little experience with building modern web applications using javascript it was quite a challenging experience. And I guess the whole point of the assignment was to judge how open I am in terms of accepting to work with new technologies and my ability to quickly ramp up on things. Nevertheless I enjoyed working on the application.
I had a little exposure to Node JS and MongoDB during one of my academic projects, but the other two Express JS and Angular JS were very new to me.
So the first order of business was to understand the importance of each of these technologies and how they fit in together for a web application to come to life.
So lets dive into it right away.

Node JS

When you go visit the about page of Node.js the first example they give you is to create a simple HTTP server. I feel this kind of sets an impression on a beginner that Node.js is used for creating a web server. But that is not entirely true. Creating a server is just one of the many capabilities Node.js offers. Node.js is a platform. It is a platform which allows you to use its libraries to build a web server and that is just one application.

So why Node.js?

  • It buys true platform independence. Like Java, Node.js also offers a write once run anywhere kind of environment and this is important not just for your production targets but for your development platforms as well. Linux users and windows users all can publish to a common target without coming across platform specific issues.
  • Its single threaded and event based so it is fast even when handling lots of requests at once.
  • It has a large number of packages accessible through NPM, a package manager. It includes both client and server-side libraries/modules, as well as command-line tools for web development.
Next up Node.js uses what are called as CommonJS modules. Now what are CommonJS modules you ask? It is mainly used to solve the problem of Javascript's single global namespace. Here is an article which talks more about that. So Express JS is actually a module which we will require in for our MEAN stack application. So how do you require in a CommonJS module? We will look at this in the next part where I also speak about our next item in the stack, Express JS.