The development of a REST API is the same as the development of every other Node.js application. We need to plan it and carefully implement the different components one by one.
It's always good to have a plan before starting a new project. So, let's define the main parts of the API server as follows:
Router: We know that Node.js starts listening on a port and accepts an HTTP requests. So, we need a class that will handle them and pass the request to the right logic.
Handler: This is the place where our logic will be put in. It will process the request and prepare the response.
Responder: We also need a class that will send the result to the browser. Very often the API has to respond in different formats. For example, XML and, at the same time, JSON.