API

📁 See the API Folder →

In order to display the products stored in the database, we need to implement an intermediary capable of retrieving the data from the database and sending it to the website. This intermediary is the API, and it works in the following way:

  • The Client asks the API to send him the information of one or more products posts;
  • the API will retrieve the data from the DB;
  • the API responds to the Client by sending him the posts he requested;
  • The Client can now display the products posts to the end user of the website.

In order to query the Postgres database, the API will be built on NodeJS and will use the Express framework.

Access to DB

In order to access the DB, the API uses a configuration file containing the database information (host, name, port, credentials). This configuration file gets those information by picking them inside environment variables. 📃 See the DB configuration file db.js

API Requests

📃 See JS file containing API requests index.js

GET all posts

To retrieve all posts, the API sends the following SQL query:

SELECT * FROM posts ORDER BY id ASC

The request is then stored in a variable in JSON format.

GET a post based on its rank

To retrieve a post based on its rank, the API sends the following SQL query:

SELECT * FROM posts WHERE id = <rank_of_post>

The request is then stored in a variable in JSON format.

GET API Health

Now that the API is able to retrieve posts from the database, it also needs to send its health status. Healthcheck are used by infrastructure tools like AWS Target Groups that checks every 15 seconds if the API is up (OK) or down (KO). If the API is down, the tools will forcibly destroy the API and recreate a functional one.

Dockerize it

All that’s left to do is containerize the API with all the necessary configuration. 📃 See Dockerfile