Building a REST API
Now that we have a basic understanding of Express, let’s build a REST API for the microblogging platform. We will start with the basic CRUD operations and then we will add more features.
In Chapter 9, we learned about the principles of building RESTful APIs. We will apply them now. As the platform is called “whispering” and the users will be able to create, read, update, and delete whispers, we will have the following endpoints:
GET /api/v1/whisper
: Get all the whispersGET /api/v1/whisper/:id
: Get a whisper by IDPOST /api/v1/whisper
: Create a new whisperPUT /api/v1/whisper/:id
: Update a whisper by IDDELETE /api/v1/whisper/:id
: Delete a whisper by ID
In this case, we used the prefix /api/v1/
because we are building the first version of the API. It is a good practice to version the API in the URL because, in the future, you may want to introduce breaking changes, and it will be hard for your consumers to adapt...