Creating a Simple REST API - Part 1
Tags: beginners, node, javascript, webdev
Published: 2022-02-15 13:28:08

Hello JuxtaCoders! 
A few months back I was learning Backend Development for a Training Program I enrolled in. We had to work on REST APIs using NodeJS and Express (with a database of your choice). The issue was, there was an overwhelming amount of resources available online, and I was a beginner. I didn't know which resource to follow, which one is better than others and why, etc. But finally, after a few weeks, I managed to make a basic REST API, using MySQL as the database (because I already knew MySQL and didn't want to learn another new tech in such a short amount of time). So, I'm bringing this 3-part series to help any beginner understand the making of a REST API better, using MySQL, NodeJS, and Express.

_<u>This series covers the following topics:</u>_

[1. Understanding what are RESTful APIs
](https://dev.to/noopur0210/creating-a-simple-rest-api-part-1-45mj)
[2. Introduction to Express.js
](https://dev.to/noopur0210/creating-a-simple-rest-api-part-2-48a)
[3. Building a RESTful API using Express.js
](https://dev.to/noopur0210/creating-a-simple-rest-api-part-3-2pfi)

---

## In this part, we'll get a quick understanding of RESTful APIs (also known as RESTful services).


We know that all web applications work on a **Client - Server Architecture**. These components interact via the **HTTP protocol**. The Server provides some services that the Client can access via the HTTP protocol. The Client can directly ask for these services through an HTTP Request and in return the Server sends a Response. This is where REST comes into use. 

REST is short for **Re**presentational **S**tate **T**ransfer. REST is a convention to handle these HTTP requests. It provides basic principles to **C**reate, **R**ead, **U**pdate and **D**elete data via the HTTP protocol. These operations are collectively called **CRUD** operations.

Let us take an example of a movie rental system :

- Let the domain name of the application be ⇒

<aside>
⚡ moviez.com

</aside>

- If the clients want to access the 'customers' page and perform the various CRUD operations on it, they may use the following HTTP request ⇒

<aside>
⚡ http://moviez.com/api/customers

</aside>

- The various components of the url are ⇒
    1. **http://**  —> Mentions the protocol used
    2. **moviez.com**  —> Is the domain name
    3. **/api**  —> This is not compulsory but can be used as a convention to determine the use of rest operations
    4. **/customers**  —> This is the end point, or what we call the 'resource'. This specific end point refers to the list of customers. The client sends various http requests to such end points.
- The type of http request determines the kind of operation carried out. For this we have a few verbs/methods that specify what operation is the client looking for, based on the http request. These methods are :
    1. **Get**    —>   for Reading Data
    2. **Post**    —>   for Creating Data
    3. **Put**    —>   for Updating Data
    4. **Delete** —> for Deleting Data
    

---

- Let's further analyze the http methods :

The following are the basic http requests that a client will make ⇒
![Possible URLs for various API requests](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cvtfsj95g0n83go17hun.png)

This is how each of them will be responded ⇒

1. First request will be to Get the details of all customers, by simply reaching the **/customers** endpoint. In response, we show the complete array of objects.

![GET request for details of all customers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ie4bfmylwvshumsjyq57.png)

2. The other request can be to Get the details of a single customer. For this, the client has to provide an "id" to view data of a particular customer, therefore, the endpoint is **/customers/:id** (here there can be any value in place of :id). In response, we should details of only that object whose value of 'id' property matches the value entered.

![GET request for customers based on IDs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rfjr1b562oan2cxkoxd.png)

3. The Put request consists of the id whose value is to be updated, and an object containing the set of values that the record needs to be updated to, so the endpoint is /customers/:id and an object. In response, we show the updated record.

![PUT request to update customers details for an ID](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ss5b5nmzgnmk1ujqnts4.png)

4. The Delete request consists of the id whose record has to be deleted, so the endpoint is /customers/:id. In response, we can show the deleted record.

![DELETE request for specific ID](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kg5x211tuvhl95wo05dj.png)

5. The Post request doesn't need any id but needs an object containing values for the various properties that need to be added to the array, so the endpoint is /customers. In response, we show the object added to the array.

![POST request to add details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hc466d4zq882oigt51u4.png)

---

That's all for now!! I hope this was helpful. In the next part, I'll give an introduction to Express and how we can set it up to write API queries that we learnt here.

If there are any suggestions, feel free to share:)

Until next time... Happy Coding!