Node.js: creating a REST server using the Express Framework in a MVC Approach

Hello, it’s been a long time. I am without any free time because of my master’s degree project. In this post I will talk about a piece of a project that I an developing, in which I had to create a REST server with Node.js.

I am a big fan of CakePHP, because it is the first web framework that I used. Because of this, I always try to find similarities with CakePHP when I start developing a project or learning a language. So, I tried to keep the server structure to be the most like CakePHP as possible.

obs: if you are not familiar with the MVC pattern, read this introductory article.

In this project, the most of models methods use the Bluebird package for promises. Furthermore, I chose to use the MySQL database. If you want to use other database, some changes may be needed to the model layer.

The full source code of the server may be downloaded here.

Application Basic Structure

If you are already familiar with the MVC pattern, you will easily understand the directory structure. There is a directory for the controllers and one for the models, but no one for views. I chose to send data from the server only using JSON. If you want to support mode format, like XML, you may use an additional views layer, separated from the controllers.

Before running the server, edit the config/config.js file, with your database configurations and the port to be used by the server. You also may specify the port using the environment variable PORT.

Routes

The Express framework already have a very good routing system, so only a few work was needed here.

Each controller has a addRestRoutes method, which add the basic controller’s routes automatically. These routes map to actions in the Controller class. Each controller of your application must inherit the Controller methods.

The default routes are for an hypothetical BlogsController are

  • GET /blog/1: maps to findById
  • POST /blogs/searches: maps to search
  • POST /blogs: maps to create
  • PUT /blog/1: maps to edit
  • DELETE /blog/1: maps to delete

The search, create and edit actions require some parameters to be passed by the request body.

To the search resource, these parameters must be like the following.

The create and put parameters are only the data to be stored in the database.

If your controller must use some route out of this pattern, you may use the addCustomRoutesmethod.

Controllers

The basic structure of a controller is

The file must be named CarsController.js and to be inside the controllers folder.

Models

The models need some basic configurations in order to use the methods of the Model class.

The main methods inherited from the Model classes are listed below.

Model.find

Do a SELECT query on the database.

Usage

Parameters

  • conditions : array of conditions in the squel format
  • limit : maximum number of records to be returned
  • offset : number of records to be skipped
  • group : SQL group by clause
  • recursive : deep level to look inside the model’s associations

Model.findById

Search for a record with the specified id. Its only parameter is the id to search for. This method returns a rejected promise if there is no record with this id.

Model.save

Create or update an existing record.

Usage

Model.delete

Remove a record from the database. Its only parameter is the id of the record to be removed.

Leave a Reply

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