Development of API using Node and Express (Part 3)

Abdulfattah Hamzah Atanda
5 min readJun 14, 2020

--

Part One of this write up can be found in Development of API using Node and Express (Part 1)

Part Two of this write up can be found in Development of API using Node and Express (Part 2)

Running App with Nodemon in development

While developing your application, it won’t be smart enough to continuously start your application every time you make changes to your code before it reflects. Hence, nodemon is a node module that watches for changes in your code and restarts your app automatically so that your changes can now reflect in the application. Install nodemon as development de[endency. A script to start development server can be defined as shown below using nodemon

To start the development server, run npm run dev:start

Validation

Firstly, create a folder named middleware in the root folder and products.js file inside it. Validations will be done inside the middleware file. The created product request body will need to be validated to check if the required data was passed to the request body or the body object is empty in order to prevent our app from crashing due to database error. Below is the validation middleware

middleware products.js

A function addProductMiddleware was created with three arguments (request, response and next) which are exposed by express. The next argument is a callback function that is called when the app is needed to route to process another function declared in the route definition after the current function definition has processed completely. In the above function definition, the name and description data were destructured from the request body and checks were made to validate if both data exists or its a string. If any of the checks is true then an error is returned and sent to the client with HTTP status code 400. If any of the checks is true, the request is terminated at that point with the error message sent to the client. If none of the validation check is false, then the next function was called to move to the next function defined in the route.

Now, our route definition will now include the middleware which will be placed in the definition ahead of the controller function as shown below

routes product.js

Let’s test the endpoint again on postman without adding the product name in the request body. The response gotten should be this

Writing Test

Writing tests for your application is a good practice that should be embraced to become a great engineer. As you scale your application, it will become difficult to find out any breaking changes whenever any new changes are made to your application. So, with tests, this eases that and gives you the assurance that no breaking changes due to new changes and if any, you will get to know from your tests.

In this project, mocha and chai will be the test framework and assertion library respectively to be used for testing the endpoints. Install mocha, chai and chai-http as development dependencies in your application. Create a tests folder and products.js file inside it. Below is the test cases definition for the add product endpoint in the tests products.js file

tests products.js

The app instance, chai and chai-http is imported. The chai assertion library will be using the chai-http which provides an interface for integration testing and provides chainable api to specify your choice HTTP request method to be invoked. Chai provides three different assertion styles which are Expect, Assert and Should styles. However, in this project the Expect style is being used, that’s why the expect style was destructured from chai;

tests products.js

The describe method is used to describe the test with two arguments. The first argument is the test description which must be a string while the second argument is the callback function. Note that you can have nested describe while writing your tests. The It method is used to describe test cases, with two arguments as well. The first argument is the test case description which must be a string while the second argument is the callback function. In the test case callback, the app instance is passed as an argument inside the chai-http request method which opens the applications server for incoming requests. Thereafter, an HTTP request is created using the HTTP method of choice (In the above, POST request method is used) with the json data attached to it using the chai-http send method. When the request is made successfully, the response returned is now checked against our expected data using the Expect assertion style.

Now define the test script to run the tests in package.json as shown below

package.json

Mocha is the test framework used to run the test, while @babel/register and @babel/polyfill were required in order to compile Es6 JavaScript syntax to compatible format. Now run the test script using npm test on the CLI, you should get something similar to what is shown below

Example Project GitHub Repository: https://github.com/fantastic-genius/products

NOTE:

While advancing your learning, for application’s model, it will be great to use an object relational mapping (ORM) framework which could be used with different types of databases when working with large projects. Some JavaScript ORM’s include Sequelize, Objection Js, Mongoose, Loopback, TypeORM etc.

Also, for validation of your request body you can as well make use of third party libraries to ease your development in a large application

--

--

Abdulfattah Hamzah Atanda

Fullstack Software Engineer skilled in NodeJs, React and React Native with interest in building robust mobile and web applications