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 ⇒
- http:// —> Mentions the protocol used
- moviez.com —> Is the domain name
- /api —> This is not compulsory but can be used as a convention to determine the use of rest operations
- /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 :
- Get —> for Reading Data
- Post —> for Creating Data
- Put —> for Updating Data
- Delete —> for Deleting Data
- Let's further analyze the http methods :
The following are the basic http requests that a client will make ⇒
This is how each of them will be responded ⇒
- 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.
- 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.
- 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.
- 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.
- 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.
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)