REST API stands for Representational State Transfer Application Programming Interface. It is a standard way for software applications to communicate with each other. This interface allows you to split your application into the backend and the frontend, with the backend in charge of providing the data and the frontend in change of interacting with the user.
The frontend sends HTTP requests to the backend in response to the user action, and the backend retrieves the necessary data and sent it to the frontend. Once the data is received, the frontend dynamically updates the interface to display the information to the user, providing a seamless and interactive experience.
So far, we've been using Express.js as a fullstack framework, which means it is responsible for handling data retrieval, as well as displaying the user interface. But in practice, it is very common for developers to use it as purely a backend program, giving us more flexible when choosing the frontend technologies.
In this lesson, we are going to discuss how to create the standard REST APIs, using Express.js and Node.js as an example.
Creating REST APIs doesn’t require any new skills, it simply states a set of standards that ensure applications can communicate effectively. REST outlines guidelines for structuring your API, handling requests, and organizing resources to maintain a predictable and accessible interface.
First of all, to set up an API, you must use HTTP methods consistently, and use them for the purpose they are designed:
GET
: Retrieves a resource.POST
: Creates a new resource.PUT
: Updates an existing resource.DELETE
: Removes a resource.
Second, the URL endpoints should be structured clearly and consistently, and they should be created around resources rather than actions. For example,
GET /posts
: Lists all posts.POST /posts
: Creates a new post.GET /posts/:id
: Retrieves a specific post by ID.PUT /posts/:id
: Updates a specific post by ID.DELETE /posts/:id
: Deletes a specific post by ID.