The Backend Beginner Series - Create REST API

Understand how to build the REST API

The Backend Beginner Series - Create REST API

Be it a MEAN/MERN stack developer or the one who loves Java, this post is for everyone who is trying to learn REST API development or is GOING TO in upcoming time. We'll talk about the jargons with utmost simplicity like architecture patterns, REST APIs, databases, associations etc in this series of blogs.

Use case: Build a solution for a hospital to manage and view patients, their address and return patients vs city data too.
Database : Relational database like MySQL, PostgreSQL etc.

Let's understand the path to solution:

  1. Manage and view patients: Create, Read, Update and Delete data related to patients. This is known as CRUD operation and is a jargon used to classify operations done on a database. Suppose you have to add the details of a patient into the system, this can be referred to as CREATE operation. To view or fetch a patient's data refers to READ operation. Some systems are read intensive such as a blogging website i.e. there are more READ operations done on the database as compared to CREATE or UPDATE.

  2. Store patient address: This deals with storing data regarding address of patients. This data is related to the patient - here comes the association of tables using foreign key etc. You need to establish relationship between the tables having patient and address data.
    One patient can have only one address => one-to-one mapping
    A question may arise into your mind - Why should we do mapping of tables to each other?
    There are few reasons like consistency, reduce redundancy of data etc.

  3. We now need a way to communicate to the database to store and fetch the details. But how?
    Here comes into play - the REST API. Consider it as a messenger between you/front-end and the backend. It will take your data to the backend and also the request type involved. The backend application will decide what should it do with the data.
    For example - You give the messenger(REST API) your email ID and password. The messenger takes it to the authentication module of your backend application. The backend will match your details with the stored data and respond you with the message and the data required.
    In case of authentication, many things like JWTs, session management etc. come into play, but we'll talk about them in a separate blog.

Let's take down the demon - piece by piece :)

  1. Create the data models - the objects that depict real life entities like patient, address. Map them to each other if required - map address table to the patient table with the patient ID

carbon (1).png

  1. Create routes and map them to functions that will handle the client requests.

carbon (2).png

  1. Write functions that will handle the request sent by client. This is done by the controller.
    carbon.png
  2. Final step : Listen to your application on a particular port and check responses through Postman.

carbon (3).png
All these steps will make sure that your REST API is made successfully. Now, you can make request from the Postman and see how your application responds. SAMPLE REQUEST image.png