Restful API with NodeJS, Express, PostgreSQL, Sequelize, Travis, Mocha, Coveralls and Code Climate.

See it as a medium that helps us interface with our book model.

In the Services folder, create a file: BookService.


ControllersThe Service file created above is used in our controller.

Create a file called BookController.

js in the controllers folderThis is the content:c.

UtilitiesAs seen, in the BookController.

js file, we imported a file called Utils.


This file contain all the responses we will assert for while running and testing the api endpoints.

Create the Utils.

js file in the Utils folder.

Its content is:d.

RoutesCreate the route file called BookRoutes.

js in the routes folder.

Its content:e.

Editing the /book-app/api/index.

js file to add the route file defined above:Step 7Running EndpointsTime to see results!We can test the endpoint using postman app or any API testing tool of your choice.

Start up the server, if you terminated it before:npm run devIf you dont know how to use postman, you might need to learn the basics, before continuing.


POST a book:In the postman address bar enter this:http://localhost:8000/api/v1/books //POSTChange the http method to POST, then define the json data to post in the bodyAs an example:b.

GET all bookshttp://localhost:8000/api/v1/books //GETc.

GET a particular bookhttp://localhost:8000/api/v1/books/:id //GETd.

UPDATE a particular bookhttp://localhost:8000/api/v1/books/:id //PUTe.

DELETE a particular bookhttp://localhost:8000/api/v1/books/:id //DELETEStep 8Write Test Cases for EndpointsI hope, all your endpoint worked as expected.

Now, let’s write test cases for each endpoint.


We will need to install testing framework like mocha and assertion library like chai and nyc for test coverage.

From the terminal run:npm install –save-dev mocha chai chai-http nycb.

We will create a test folder in the path /book-app/api/.

Then create the test file, call it test.

js .

Hence, the file is located: /book-app/api/test/test.


Include the test script in package.

json fileReplace:"test": "echo "Error: no test specified" && exit 1"With:"test": "export NODE_ENV=test && sequelize db:migrate:undo:all && sequelize db:migrate && nyc –require @babel/register mocha .


js –timeout 20000 –exit"Observe this:export NODE_ENV=test //Works for mac OSUsing Windows, do:SET NODE_ENV=test //works for windowsRemember, we have a test database we created earlier: book_test So, from the test script, we run migrate afresh each time.

Before you run the test, ensure that PostgreSQL server is running.

Save all files and run the test:npm run testA Sample output:All test cases passing!Step 9Integrate Travis CI, Coveralls and Code ClimateThis i presume, you have been waiting for.

Ensure that all test cases are passing, because Travis will also run those tests.

So if any is breaking in your local, it will also break in Travis.

Before we proceed, let’s compile all we have and put them in one folder called build in this folder all ES6 is converted to ES5 by babel .

Include the script immediately after the test script in package.

json"build": "rm -rf .

/build && babel -d .

/build .

/api -s",We should also include scripts for coveralls and code-climate"generate-lcov": "nyc report –reporter=text-lcov > lcov.

info","coveralls-coverage": "coveralls < lcov.

info","codeclimate-coverage": "codeclimate-test-reporter < lcov.

info","coverage": "nyc npm test && npm run generate-lcov && npm run coveralls-coverage && npm run codeclimate-coverage"The script should look like:"scripts": {"dev": "nodemon –exec babel-node .


js","test": "export NODE_ENV=test && sequelize db:migrate:undo:all && sequelize db:migrate && nyc –require @babel/register mocha .


js –timeout 20000 –exit","build": "rm -rf .

/build && babel -d .

/build .

/api -s","generate-lcov": "nyc report –reporter=text-lcov > lcov.

info","coveralls-coverage": "coveralls < lcov.

info","codeclimate-coverage": "codeclimate-test-reporter < lcov.

info","coverage": "nyc npm test && npm run generate-lcov && npm run coveralls-coverage && npm run codeclimate-coverage"},For this project, the final look of your package.

json file should be:Now let’s create the build folder by running:npm run buildYou will see a folder in the path: /book-app/book-app├── api├── builda.

Sign-Up/Sign-In with TravisCreate an account with Travis if you don’t have one already;b.

Create a .


yml file.

This is the file Travis will look for and execute commands.

This file should be created in the same path as the package.

json That is: /book-app/Populate the file with:Observe that there is a script to create a database, a user , run migration run build npm run build.

Also observe the test command npm testc.

Push your code to github:To see the workings of Travis, Coveralls and Code Climate, push the code you have been working on to a new git repository or an existing one created for this project.

After pushing to github, go to your Travis account and turn on that repository:An example:Click on the repository, It will take to a page like this:You may likely see this badge first:travis_unknown_buildIf your build does not start automatically, look at the top right of the screenshot above, you will see More options choose the Trigger build , then a modal pops up, click the Trigger custom build .

This will start the build manually.

A mail will be sent to you when the build is completed.

You can check the build status again, you should see the green passing badge.


Use the Travis badge in your github repositoryClick on the Travis badge, a modal pops up, Choose markdown and copy the link and paste in the README.

md file of your repositorye.

Create an account with or sign-in to your CoverallsConnect the same repository we used for Travis by turning it on.

Then click on DETAILS.

Scroll down, copy the badge code when you click EMBED and add it to your README.

md filef.

Create an account with or sign-in to your CodeClimateOnce you are in, Add the same repository you have used in the two cases above.

Once you click on the repository, it will take you to a page similar to this:Code maintainability can have status A to FThe A status shows that the code can be maintained easily, while a status of F tells the opposite.

To get the badge code, click on the Repo Settings tab.

Click on the Badges.

click the Markdown, copy the code and add it to your README.

md fileNote:Let me bring to your notice that Code Climate also have Code Coverage.

Since Coveralls is already covering our code, i didn’t see it necessary to add Code Coverage.

If you wish to add it, Click on Test Coverage and follow the instructions.

So, Your README.

md should look similar to:[![Build Status](https://travis-ci.



org/victorsteven/Book-app-NodeJS-PostgreSQL-Travis-Coveralls-Code-Climate) [![Coverage Status (https://coveralls.



io/github/victorsteven/Book-app-NodeJS-PostgreSQL-Travis-Coveralls-Code-Climate?branch=master) [![Maintainability](https://api.



com/github/victorsteven/Book-app-NodeJS-PostgreSQL-Travis-Coveralls-Code-Climate/maintainability)With the output:Bravo!Step 10:Final testing and scaling the apiWow!.This article is about 12 minutes read.

I admire your patience to see the very end.

However, I can’t call this the end because, there are still some stuffs to do, which, you wont have issues executing i guess.


You can host the application on Herokub.

You can create more endpointsc.

You can write more test casesd.

And lots more.

ConclusionI hope you didn’t have too much trouble following the instructions in this article.

This article is very basic, meaning that you can apply the knowledge gained here to any similar project you are currently working on.

Get the source code here: githubThank you!.. More details

Leave a Reply