[Node.js 101, MongoDB 101] Small COVID19 API

In this article, as a Distributed Systems 2020–21 Class project, we will make a simple COVID19 app where will shows us specific data from a MongoDB database. Keeping in mind that exist many different ways to make this work, where in this article we will show a first attempt to make this done.

As asked by the teacher, the objectives are the following:

[ Getting Started ]

Having the Visual Studio Code and MongoDB installed on a Windows 7 machine, we will start the VSCode and on the left of the window, on the explorer, we will create a new folder named SmallCOVID19Api.

Right-click on it, and create a New File named index.js that later we will work on it.

Right-click again on the folder item and click Open in Integrated Terminal an execute the following command to initialize the project:

npm init

For now we will only insert the author, but you can fill all the fields when asked or change later on the file package.json that will be created after this step is done. This one will keep info and dependencies about the project.

When finished you will have something like the following:

Now we will install all the modules needed to make this project work executing the following line on the terminal:

npm install -save express body-parser mongoose

This command will install that specific modules:

  • Express.js (minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.);
  • body-parser (responsible for parsing the incoming request bodies in a middleware before you handle it. This one can be used to manipulate JSON solicitations);
  • Mongoose (provides a straight-forward, schema-based solution to model our application data, in other words will be the database support of the project).

At the end you will have something like the following:

To make the project in a organized way, we will follow the MVC a software design pattern commonly used for developing user interfaces that divides the related program logic into three interconnected elements:

  • Model, the code for our database model;
  • View, the layout;
  • Controller, the logical code.

So, using the UI of the VSCode, right-click on the smallCOVID19Api directory and we will create three new files named model.js, routes.js and controller.js that later will insert some code.

[ MongoDB]

On the making of this project, the database part was worked directly with the mongodb cmd client where the data was insert manually in a way to a understand a better use of the MongoDB. There is others ways that can be done in a more ‘automatically way’ where in the future will maybe have a article that show how to insert data into a mongodb using the api directly, but as early mentioned, this was the first attempt to making this project work.

To continue, navigate to the MongoDB installed directory, specifically to the …MongoDB\bin path and open a Windows Command Prompt there and execute the command mongo.

You can see existing databases by using the command show dbs but for now we will create a new one named smallcovid19api using the following command:

use smallcovid19api

Now we will create a collection named datainfo that allow us store documents. Execute the following line to create it:

db.createCollection('datainfo');

As above for the databases, you can see the collections that a database have by using the command show collections. Until now, you will have something like this:

Now we will need some data to put in our database. For that we will visit a VOST website that have a bunch of requests related to the COVID19 pandemia.

VOST (Voluntários Digitais em Situações de Emergência) is a portuguese voluntary group from Portugal, defined as Digital Volunteers in Emergency Situations. You can know more about them in https://vost.pt/.

To continue we will visit their website https://covid19-api.vost.pt/ and as asked by the teacher from the Distributed Systems 2020–21 Class we will need data from 29–03–2021 until 03–04–2021 (as this last is not inclusive on the request, we will use 04–04–2021).

Keeping in mind the objectives, we will take the pretended fields that we want as they are: registo, data, confirmados_novos and internados_uic between 29–03–2021 and 03–04–2021.

As said early, there is a lot of ways to make this procedure in a more ‘automatic way’ like the API could get all data and have more code to filter dates and etc… making our work easily and maybe soon will be a article showing some ways to make that happen, but for now, we will continue this way, as this was the first attempt to make this API.

Returning to the MongoDB and using a text editor, we will create a INSERT to fill our smallcovid19api database, more specifically the datainfo collection, that was early created with the pretended data.

And execute it in the mongodb client command prompt window that will show a output as similar like this:

You can check the new data inserted by using the following mongodb command:

db.datainfo.find().pretty()

Now that we have a database and some data on it, we will proceed to continued the development of the api.

[ index.js ]

Returning to the VSCode we will put the following code into the index.js file:

[ model.js ]

In the model.js we will put the following code:

[ initializing the routes.js ]

For the beginning of this part, we will initialize the routes.js file with a already welcome page and a existing GET function (index) for a route, with the following code:

[ initializing the controller.js ]

In the controller.js, we will initialize it with the working function called index, that with some lines of code, will show us all data that we early insert into the database smallcovid19api/datainfo on MongoDB. For that we will put the following code:

[ Testing ]

For testing purposes, we will use POSTMAN, a collaboration platform for API development. Now, on the VSCode terminal, with this one on the project directory path, let’s start the server with the following command:

node index.js

As the server is running, in the POSTMAN we will send a GET request for the default address early coded of the API (localhost:3030/data). If everything is correctly done, you will get something like the following:

Next, let’s try our first function of the API that will show us all the data that we early insert into the MongoDB database, the index one. For that, send a GET request to the address localhost:3030/data/index and you should get something like this:

Remembering the objectives that we have, now let´s make each function for each one. As show showed early, each function will be added into the controller.js file and them adding the necessary code related to the respective routes in the routes.js file.

Note: Remembering that as we only inserted data related to 29–03–2021 until 03–04–2021, the following functions will take into account all data existing in the database as a week.

[ 1# List number of new cases (for all days of the week) ]

For this first one we will use the following code do get a list with the new cases of the week:

[ 2# List number of intensive care admissions (for all days of the week) ]

For this second one we will use the following code do get a list with the intensive care admissions cases of the week:

[ 3# Day with more new cases ]

For this third one we will use the following code do get the day and number with the maximum new cases of the week:

[ 4# Day with fewer cases ]

For this fourth one we will use the following code do get the day and number with the fewer new cases of the week:

[ 5# Average 7 days ]

For this fifth one we will use the following code do get the average of new cases in seven days of total:

[ 6# Total number of new cases in the week ]

For this sixth one we will use the following code do get the total number of new cases from that period:

Now, that all the objectives totally accomplished, let’s finish with a new one, where it will show all the data that we want with the following code:

Hope you liked this small article, best regards, Ricardo Costa (Richards).

This article was created in a context of the Distributed Systems Class 2020–21, ESTG-IPG.