Hello, you are welcome to read this post. I’m sure most of you reading this post have made use of an app or website that gives you updates on the spread of the Corona Virus. You might have asked yourself the question of how these applications have access to this data.
How this happens is that they make use of something called an API (Application Programming Interface). Basically, an API is an interface that allows two applications to communicate. They can be in the form of a URL, a native method on a device etc.
In this post, I would be describing how to build your own API that can provide access to the information regarding the Corona Virus.
Hence, let me describe how this build works:-
- We would scrape a website called Worldometer. It provides realtime stats on various data.
- Once we scrape the data, we parse the HTML elements to get what we want
- We use the data to construct an object
- Then we build two endpoints to respond to requests.
Create a new directory and type “npm init -y” in the command line.
Run npm install express, node-fetch, cheerio. These are the dependencies we would use to build this API.
Express would help make handling of request easy, node-fetch would give us similar methods to the fetch API on the browser which most devs like, cheerio is the library we would use to parse the HTML returned from using the node-fetch library to make a request to the site.
Before we continue I would like you to know that I used my chrome dev tools to inspect the site (https://www.worldometers.info/coronavirus/).
The reason for this is because for me to parse the data in the HTML correctly, I need to know where each required item is located in the HTML tree.
The cheerio library used looks a lot like jquery. So this post would be easy for you to understand if you’ve used jquery.
The block of codes below contains the details and implementations used to make this API with comments that helps explain what I did.
Create a file called data.js, it would contain the code that handles fetching of the data for a specific country.
Create a file called countries.js, it would contain the code that handles fetching of the data that displays countries that you can query for
Finally, create the index.js file. This file would handle the routes and request from a client.
Then run “node index.js”. This would start the server and you can try and make a request from your browser.
To get the Corona Virus information for a specific country. Run this in your browser:- localhost:3000/{your country}
To know the countries you can query for using this API, run this in your browser:- localhost:3000/list/countries
Thanks for following through, please if you have any contribution or question you can leave that in the comment section. Thanks
You can check out this GitHub repo to see the files used in this post by clicking here
Princess Oviawe
Awesome write up.
I haven’t done this before, but I Found it easy to follow.
Sammy
Thanks dear.
Mike c
This is cool Sam. I like that steps are easy to understand
Sammy
Thanks Mike