Working with the MEAN stack: Application setup

In this tutorial I will convert the app I wrote using AngularJS to make it use the MEAN stack. Don’t worry, there’s nothing mean about the MEAN stack.
The MEAN stack is a pure JavaScript stack containing:

  • MongoDB: MongoDB is a NoSQL database allowing you to store JSON (actually BSON) documents and query them. This is a great database when going for pure JavaScript applications because it’s easy to put the same object structure that you’re working with, inside the database.
  • Express: Express is a web framework that runs on Node.js. A “web” framework may sound a bit vague, but it is actually that. It allows you to serve static content, it allows you to route certain requests to execute certain logic (great for RESTful webservices) and so on.
  • AngularJS: I probably don’t have to explain this one to you, but AngularJS is a client-side Model-View-Controller framework by Google.
  • Node.js: I mentioned it already that we’re going to use Node.js (because we’re going to use Express), but Node.js is a JavaScript platform, quite popular nowadays. In this tutorial we will use it as our server-side platform.

Prerequisites

Before starting with the application, you obviously need to install the MEAN stack. If you followed some of my other tutorials, you probably already used Node.js and npm to use Bower. If you didn’t install Node.js yet, you can get it from nodejs.org.
By installing Node.js you install two things; first of all it will install Node.js (obviously), and second it will also install the Node.js package manager called npm.

Verify that the installation was successful by using the command:

 

 node version 

 

or:

 

 nodejs version 

 

And to check if npm was installed, use:

 

 npm version 

 

The second step is to install MongoDB, which you can download from mongodb.org. To check if the installation was successful, use:

 

 mongod version 

Setting up our application

The first step is that we’re going to setup our application. Start a new project and add the following folders:

  • client: This folder will contain our client-side application using AngularJS. This content will be served by Express.
  • config: This folder will contain configuration used to run our application. Things like a connection URL and the port we will run on can be setup here. It’s a good idea to centralize this information for when you want to dpeloy the app on a cloud based environment.
  • server: This folder will contain our server-side logic to serve the client and to provide a RESTful webservice.

We’re also going to add some files. First of all we’re going to create our Node.js application by creating a file called app.js. Node.js also comes with a packaage manager called NPM.
To load our dependencies, we will have to define a file called package.json, which will define our application and the dependencies we have.
In our app the file will contain:

  1. {
  2.   “name”: “mean-song-rate”,
  3.   “version”: “0.0.1”,
  4.   “dependencies”: {
  5.     “mongoose”: “~3.6.14”,
  6.     “express”: “~3.3.4”
  7.   }
  8. }

I already explained what we’re going to do with Express. Mongoose on the other hand is a Node.js framework used to communicate with MongoDB. It provides a sweet API where you use models to add/update/retrieve and read your data.

project-setup

 

Client

For the client-side part of our application, I’m going to start of where we left in my previous tutorial. I’m not going into detail about AngularJS controllers, services or directives in this tutorial. If you’re interested in that, I suggest reading my introduction tutorial series to AngularJS.

What we do need is the code of the application, which you can find on Github (master.zip). Put all the code in the client folder, except bower.json and .bowerrc which you can put in the root folder.

Now open up .bowerrc and change the directory to:

  1. {
  2.   “directory”: “client/libs”,
  3.   “json”: “bower.json”
  4. }

 

This is obviously necessary because we moved our Bower configuration one level up (to the parent folder).

Now open bower.json and add the dependency angular-resource, for example:

  1. {
  2.   “name”: “mean-song-rate”,
  3.   “version”: “0.0.1”,
  4.   “dependencies”: {
  5.     “angular”: “1.2.15”,
  6.     “angular-resource”: “1.2.15”,
  7.     “bootstrap”: “3.1.1”,
  8.     “lodash”: “2.4.1”,
  9.     “underscore.string”: “2.3.3”,
  10.     “font-awesome”: “4.0.3”
  11.   }
  12. }

Angular-resource is a part of the AngularJS framework and provides an easy way to integrate with your RESTful webservices.

Also note that I changed the name in the bower configuration.

project-setup-client

Configuration

The next part is the configuration. Open the config folder and add the following files:

  • config.js: This file will contain our main configuration including port number and database URL.
  • db.js: This file will contain the Mongoose configuration to connect to our MongoDB instance.
  • express.js: This file will contain the configuration used for Express.js like on which port it has to un, which context will be used to serve our client application and which context will be used to provide our RESTful webservices.
  • routes.js: Finally we will also use the routing pattern to setup which URL is bound to which logic in our controller.

project-setup-config

Server

The serverside logic will not be too complicated. We’re going to add two folders here called controllersand models.

In the controllers folder we will add our RESTful webservice controller, called rest.js. In the models folder we will add our model, a file called Song.js.

project-setup-server

Writing your Node.js app

Now all files are ready, so let’s start by writing our application. I’m going to start with the boring stuff first, namely the configuration part.

Let’s open up config.js and add the following code:

  1. var env = process.env.NODE_ENV || ‘development’;
  2. var config = {
  3.   port: 3000,
  4.   db: ‘mongodb://localhost/songs’,
  5.   host: ‘localhost’
  6. };
  7. module.exports = config;

On the first line we’re retrieving a system variable called NODE_ENV. This is not really important in our app, but if you want to deploy this on a cloud environment like IBM BlueMix or if you want to host this on multiple environments, you might be interested in this. For every environment you choose another value forNODE_ENV, which will allow you to use something like:

  1. if (env === “production”) {
  2.   config.db = ‘mongodb://my.production.server/songs’;
  3. }

So, on the next lines we can see our configuration which contains:

  • port: The port our application will run on
  • db: The MongoDB connection string
  • host: The hostname the application will run on

And finally we make sure that this module returns config, so when another module calls it, it gets the configuration object. You can do this by using module.exports.

The next configuration file is db.js. Similar to config.js we will configure Mongoose and return the configuration by using module.exports, for example:

  1. var mongoose = require(‘mongoose’);
  2. module.exports = function(config) {
  3.   mongoose.connect(config.db);
  4.   var db = mongoose.connection;
  5.   db.on(‘error’, function() {
  6.     throw new Error(‘Unable to connect to database at ‘ + config.db);
  7.   });
  8. };

So, here we’re using the configuration object we made in our previous module and use it to connect to MongoDB using Mongoose.
The configuration itself is passed as an argument to this module, I will show you how that works later.

Then the next configuration file is express.js, similar to db.js it will configure our application based upon the configuration from config.js.

  1. var express = require(‘express’);
  2. module.exports = function(app, config) {
  3.   app.configure(function () {
  4.     app.use(express.compress());
  5.     app.set(‘port’, config.port);
  6.     app.use(express.logger(‘dev’));
  7.     app.use(express.bodyParser());
  8.     app.use(express.methodOverride());
  9.    
  10.     app.use(‘/api’, app.router);
  11.     app.use(‘/’, express.static(__dirname + “/../client”));
  12.   });
  13. };

So, what happens here is that this module accepts two parameters, app which is an application object initialized by Express and second we have config which is our configuration object which we’re going to need to know the port we’re going to run on.

So, let’s talk about each line more into detail. The first two lines of configuration are pretty easy, the first line makes sure that our files are compressed using the GZIP protocol and the second line says which port we’re going to use.

The third line (app.use(express.logger('dev')); makes sure that we’re going to log all requests of both our RESTful webservice as the static files that are served.

The next two lines are important for our RESTful webservice In our RESTful webservice we’re going to use the post body to add or update the objects. To do that we need to enable the express.bodyParser()) on our application.
The express.methodOverride() on the other hand allows us to use PUT and DELETE requests, which we will use to update and delete our objects in our RESTful webservice.

Then finally we configure our app to use /api to serve our RESTful webservice and / to provide the static files from the client folder.

The last configuration file is routes.js and this is probably the easiest one. In this configuration file we will define which REST endpoint is mapped to which method in our controller.

  1. var rest = require(‘../server/controllers/rest’);
  2. module.exports = function(app){
  3.   // find all songs route
  4.   app.get(‘/songs’, rest.findAll);
  5.  
  6.   // find one song route
  7.   app.get(‘/songs/:id’, rest.findOne);
  8.  
  9.   // Add song route
  10.   app.post(‘/songs’, rest.add);
  11.  
  12.   // Update song route
  13.   app.put(‘/songs/:id’, rest.update);
  14.  
  15.   // Delete song route
  16.   app.del(‘/songs/:id’, rest.remove);
  17. };

As you can see here, we’re using several endpoints here:

  • GET /songs: This will be used to retrieve all songs in our database
  • GET /songs/:id: The :id is a placeholder for the real ID of a song. Which means that we’re going to use this to retrieve a single song from our database.
  • POST /songs: This method will be used to add a new song to the list. The song itself will be provided as JSON in the post body of the request.
  • PUT /songs/:id: This method will be used to update a song by its ID. The updated song itself will be provided in the request body, just like adding new songs.
  • DELETE /songs/:id: This method will be used to delete a song by its ID.

At the first lien you can see how we import our REST controller in the configuration file by usingrequire('../server/controllers/rest').

The main application

Before actually writing our application we have to write the “glue” that keeps all modules together. Our main module (app.js) will import the configuration files to make sure our app is properly started.
The code itself is not that complex, as it’s sole purpose is to load other modules (like our configuration).

So, first of all we’re going to import all modules that we need by using the require() function:

  1. var express = require(‘express’), mongoose = require(‘mongoose’), fs = require(‘fs’), http = require(‘http’),
  2.     config = require(‘./config/config’), root = __dirname, app = express(), server = null;

Most of these modules are known by now. Only fs and http are new modules. These are standard modules of Node.js, so that’s why you won’t find them in package.json.
The names of the modules already explain what they do:

  • fs: Provides functions to access the file system
  • http: Allows you to create a HTTP web server

Then the next step is that we’re going to configure Mongoose by using db.js:

  1. require(‘./config/db’)(config);

As you can see here, we’re importing the module and immediately after it we’re executing the function and providing the config argument.

The next part is a bit more complex. To dynamically load all our models, we’re going to list all files in the models directory and add all modules that are inside that folder.
We can do that by writing:

  1. var modelsPath = __dirname + ‘/server/models’;
  2. fs.readdirSync(modelsPath).forEach(function (file) {
  3.   if (file.indexOf(‘.js’) >= 0) {
  4.     require(modelsPath + ‘/’ + file);
  5.   }
  6. });

Now we only have the express and route configuration file left, which we’re going to load by writing:

  1. require(‘./config/express’)(app, config);
  2. require(‘./config/routes’)(app);

And finally we need to start the webserver itself, which we will do by adding:

  1. var server = http.createServer(app);
  2. server.listen(config.port, config.host);
  3. console.log(‘App started on port ‘ + config.port);

This is everything we have to do to make our application work. In the next part I’m going to explain how we’re going to create the MVC application using Express and how we’re going to tweak the AngularJS application to make it work with our new RESTful webservice.


g00glen00b

IT Consultant with a passion for JavaScript. Experienced in the Spring Framework and various JavaScript frameworks.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *