Developing an online library – a REST API
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.
Defining the API parts
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.
Writing the base
Node.js is very often used to build REST APIs. Also, because it is a common task, we have several possible approaches. There are even ready-to-use modules such as rest...