June 15, 2014 Uncategorized

API REST Development with MongoDB, NodeJS and Express

In the previous post we analyzed an stack that currently I consider very interesting for developing modern web applications. The front-end of this stack was composed by AngularJS for the browser desktop application, and by Ionic for the mobile application. However, we could choose developing the back-end with some variants, depending on our application requirements.

We also saw the importance of choosing the proper frameworks or tools for developing our projects, because these tools could offer us an important improvement in our productivity and because they could help us developing robust applications.

However, I consider that if you want to get profit from these frameworks it’s convenient that you understand what they do internally. Our back-end will offer us an API that will be in charge of managing and querying our data. In this article we are going to analyze how to create manually a server that will serve a simple API. On subsequent articles we will see how to use some frameworks that will help us developoing our own server much quicker, more robust and with more features.

 

Our Goal

The main goal of this article is developing a simple REST API with NodeJS and Express, so we can see how easy, although tedious, is developing an API in this way. The API will allow to insert new messages in a MongoDB collection and will allow to retrieve a messages list.

You can download the source code from GitHub:

GitHub Source Code

If you want you can clone and start this application running the following commands from the command line:

Next we will explain how to develop an API from scratch.

 

Installing NodeJS packages

The first step is to create a new folder where we are going to develop our project. We have to enter into this folder and we will install the following packages with npm:

  • expressnpm install expressexpress is the most popular web framework for developing web applications in NodeJS.
  • express-generatornpm install -g express-generator. If we don’t have already installed it, we have to install it globally. We will use express-generator to create our application skeleton.
  • mongoosenpm install mongoose –save. Finally, we will install mongoose, that is an ORM that we will use to access our MongoDB database.

In summary, we will run the following commands in our application folder to install the packages that we need:

 

Create the Application

Once we have installed all our packages we have to create our express application. Inside our working folder we will execute the express command and it will create automatically our application skeleton.

Next we have to install the packages that express needs, so we will run npm install from our command line.

Finally, we will start our application running the command npm start.

In summary, from our command line we will execute the following commands for creating and running our web application:

Once our server has started, if we access with our browser to the URL http://localhost:3000 then the application will show us an express welcome page.

Optionally, as we are developing our server to manage an API, we can remove all those files that we don’t need but that has been created by express-generator for delivering web pages: we can remove the public folder content and we can remove the views folder.

 

Connecting to the database

For connecting to the database we are going to create the db folder and inside this folder we will create the index.js file. This file will manage the database connection (we should define the database connection string in a configuration file, but for simplicity and for not extend more the article we will define it directly in the code):

After that we will update the app.js file so it will stablish the connection to the database (lines 7 and 13):

 

Creating the entities

With Mongoose we are going to define our entities schema. For this demo we only define one very simple entity, called Message. In our project folder we will create a folder called models, and we have to create the file message.js inside it:

 

Creating the controllers

In our project folder we will create a folder called controllers, and we have to create the file message.js inside it. Inside the controller we will define the actions that we will be able to execute through our API. In this case, for simplicity, we are going to define only two actions:

  • list: retrive a messages list.
  • create: insert a new message in our messages collection.

 

Creating routes

In our project folder we will create a folder called routes, and we have to create the file message.js inside it. In this file we will define which methods of our controller are going to be executed when our server receives an HTTP request:

 

Finally, we will update the app.js file to add the routes that we have defined in the previous file (lines 6 and 14):

Testing the API

Finally, we have to test that our API works properly. Ideally we would create a test suite, using the superagent package for instance, but as the goal of this article is only developing a simple API, we are not going to explain how to develop these tests.

We are going to test our API manually with a Chrome extension called POSTMAN. The first time that we do a GET of messages, the API will retrieve an empty collection:

blog-ch1-empty-collection

If we want to insert a new message we have to use the POST method and we will send as parameters the message that we want to create:

blog-ch1-post

Finally, if we request the GET method again, then we will get a JSON list that will contain only the message that we have created previously:

blog-ch1-collection

Conclusion

We have seen that creating an API with NodeJS and with Express without an specific framework is a simple although tedious task. There are more features that an API must implement but that we have not explained in this article. Implementing these features manually will take quite time. For example, our API should manage entities relationship, authentication, authorization, etc.

Next articles will explain how to develop an API with frameworks and tools that will improve our productivity.

 

  1. […] the previous article I explained how develop a REST API with MongoDB, NodeJS and Express from scratch. We saw that developing an API from scratch is a simple process although it could be […]

Leave a Reply

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

Back to top