Making a small API using JSON data

NSerus
3 min readMay 31, 2021

--

Hello Peppos!!

T’day we are going to use an API to get data from covid stats in a week from a json file and present it in the following ways:

  • List number of new cases (every day of the week).
  • List number of cases on intensive care (every day of the week).
  • Dia with most new cases.
  • Day with less new cases.
  • 7 day average.
  • Total number of new cases of the week.

For any problems in the way, here’s my repository with the final result:

Preparing the API

After creating the folder that will host the project, we initiate the project with:

npm init

Also, we will need the following packages:

  • Express.js — Framework that allows developing of web apps in a simple way.
  • body-parser — Used to manipulate JSON files
  • mongoose — Database that supports the project

Install the needed packages with:

npm install --save express body-parser mongoose

Importing files to mongoDB

To import the files to mongoDB we need to open MongoDB Compass and conect after that create a directory in the following way:

After that click Add Data and Insert Document and copy this code:

After importing the data we can create our API…

Creating the API

We need first to create the index:

Make the model:

Make the routes:

And finishing up with the controllers:

After this, we can test out the API…

API testing

Testing all the functions in postman…

new cases test
avg cases test
critical cases test
max new cases test
min new cases test
total new cases test

As we can see in the testing, we have an operational covid API.

Hope you had fun!!

--

--

NSerus
NSerus

Written by NSerus

A Computer Science Student

No responses yet