node.js: API COVID-19

Afonso Antunes
3 min readApr 9, 2021

--

Hi people!!

Today, I’m going to give you a tutorial to create an API on the most talked about pandemic right now, COVID-19.

This project that we are going to create will consist of a main file — index.jsand the MVC files. The objective will be to illustrate some revealing data in Easter week, namely the new daily cases and hospitalizations in intensive care.

But before we start on the project, is necessary to install the following programs:

1st step: Create a directory for the project and enter the same

mkdir apiCovid
cd apiCovid

2nd step: Start the project

npm init

3rd step: Install the required packages

npm install --save express body-parser mongoose

4th step: Create the index.js and the MVC files

touch index.js
touch covidController.js
touch covidModel.js
touch covidRoutes.js

5th step: Enter the page where you will find all the data about COVID-19 by clicking here

This page will be very important for the creation of this API because it is where we will get the necessary data to be placed in the database.

In addition, this is the ideal page for anyone who wants to create more API’s on COVID-19 data.

For this step, we will do the following tasks:

  1. Select the 2nd get: “/Requests/get_entry/{date_1}_until_{date_2}”;
  2. Click on the “try it out” button and then on date_1 set date 29–03–2021 and date_2 set date 4–4–2021 and finally, click on execute;

WARNING: Dates must be in the format shown in the following figure

Date format

After execution, it will show all data about COVID-19 included on those dates.

3. Click download (as shown in the following figure);

The file you have downloaded will come in JSON format.

6th step: Open mongoDB Compass, connect to the database and create a database, filling in the following spaces

7th step: Enter the database you created, click “Add data”, then “import file”, select input file type “JSON” and go to “Browse” to find the JSON file you downloaded and “Import”

8th step: After importing, we will edit the database so that it looks like this

Final aspect of the Database

Now, it’s time to program our API

Note: All JS files will have their code explanations as comments. This is going to be important to understand what each piece of code does.

9th step: Open index.js and put the following code

So, let’s move on to the most important part of our API: the MVC

10th step: Open covidModel.js and put the following code

11th step: Open covidRoutes.js and put the following code

12th step: Open covidController.js and put the following code

Last Step: Open Postman and let’s put the following address: http://localhost:8888/api/covid/

The final output will be something like this:

I hope you have succeeded and that you have learned something!!

--

--