In the previous post we got started with MEAN stack. Now, let’s play with MongoDB and Express JS.
The M of MEAN (Database)
Let’s tackle the M of the MEAN stack, MongoDB. MongoDB is a NoSQL database where you store JSON documents. You can download and get started with MongoDB from http://www.mongodb.org/. We’ll create a new database called Football. So, the connection string that we’ll use is mongodb://localhost/Football. The team’s information that the user feeds in through the application will get added to the Football database. So, for instance, if the user enters the team name as Holland and captain as Robin Van Persie, our application will store the following JSON data in Football database.
{ "team":"Holland","captain":"Robin Van Persie" }
The Node module that we will use to connect to MongoDB is called mongoose. We will use it in our next section.
The E of MEAN (Server-side)
The ExpressJS is the web framework on the Node platform. As I mentioned earlier, it’s an MVC-style framework. There are a number of features and ways by which you can develop an application using ExpressJS. However, in this article, I want to shed all the flab and just focus on the essentials that would get our job of pushing the team data to the DB.
We’ll begin with developing the models using Mongoose. You can read more about mongoose from http://mongoosejs.com/. The modeling concept in Mongoose is pretty straightforward. You define a class Team with its properties. If you want to add a record, you need to create an instance of Team and invoke the synthesized methods like save.
In the app folder, let’s develop a simple model called Team. I’ll create it in a models.js file.
//models.js var mongoose = require("mongoose"); Schema = mongoose.Schema; var TeamSchema = new Schema({ name : String, captain : String }); mongoose.model("Team",TeamSchema);
As shown in models.js, we import the mongoose module, create a new schema and register it as model Team. Creating an instance of Team and calling the save() method does the needful. And where do we write that code? In a Controller.
In the app folder, let’s create a file, controllers.js with a create() method. This method creates an instance of Team and calls it save().
//controllers.js var mongoose = require("mongoose"); var Team = mongoose.model("Team"); exports.create = function(req,res){ var team = new Team({ "name":req.params["name"], "captain":req.params["captain"] }); team.save(function(err) { if (err) { return res.send(400, { message: "Error creating team" }); } else { res.jsonp(team); } }); };
So, the controller creates the model and saves it in the database. Now, we need to invoke the controller. Let’s create a URL mapping for the controller say, team/add/:name/:captain. Figure below shows the URL mapping with the controller.
Let’s configure this URL mapping using a Router in ExpressJS. We’ll create a file called routes.js in app folder that has the following code:
//routes.js var express = require("express"); var teams = require("./controllers"); module.exports = function(app){ app.post("/team/add/:name/:captain",teams.create); };
Our URL can be accessed using a HTTP POST method by passing the name and the captain of the team. Our little server code is over, but where is the server?