DEV Community

Cover image for Creating a Simple REST API - Part 1
Noopur Sharma
Noopur Sharma

Posted on • Updated on

Creating a Simple REST API - Part 1

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.

This series covers the following topics:

1. Understanding what are RESTful APIs

2. Introduction to Express.js

3. Building a RESTful API using Express.js

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 Representational State Transfer. REST is a convention to handle these HTTP requests. It provides basic principles to Create, Read, Update and Delete 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 ⇒


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

  • The various components of the url are ⇒
    1. http:// —> Mentions the protocol used
    2. —> 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

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

  1. 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

  1. 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

  1. 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

  1. 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

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!

Top comments (0)