In this module in the very first lesson,
we learned how to build a full-fledged REST API server using express.
So we're able to service the GET, PUT, POST,
and DELETE requests coming to the various REST API end points.
But, the server itself was simply returning
a simple message in response to these requests.
In a real REST API server,
any incoming request will entail a corresponding operation to be performed on the back in
the database perhaps to retrieve data to respond to a GET request,
or maybe to modify data that exists on the server in response to a PUT request.
Now in the rest of this module,
we have studied about how we can interact from a node application with a MongoDB server,
be it using the MongoDB driver or using Mongoose.
Now, a full-fledged REST API server that is able to handle
the request end to end will be possible only when we combine the two together.
That is, an express-based server that does
all the business logic processing and at the same time will issue
the database requests to the MongoDB
using the node MongoDB driver or using Mongoose.
So how do we combine the two together?
So this is what we will look at in this particular lesson,
and the two exercises that we will do as part of this lesson.
We now have learnt how to build
a REST API server using Express
and service the various requests coming to the REST API end points.
We have also seen how we can interact with the database from our node application.
Now, given that you have a GET request coming into the server as an example,
to handle this GET request end to end,
a GET request coming from the client means that the client wants to retrieve
data from the server and use that data.
So, a GET request coming into the server will have to be
handled through the various processing done by, for example,
the Express server and once the processing is done then
Express server's business logic realizes that it needs to
perform a query operation on the database.
So, this may initiate a query to the database in order to
fetch a set of documents from the database,
and then the retrieved data will then be
transformed into a reply message and then sent back to the server.
So this end-to-end handling of the request and response involves two parts.
One of course doing the business logic in
the Express server and then doing the interaction with
the database from the node application from Express server which is
a node application using either MongoDB driver or Mongoose.
We will be using Mongoose in the exercises.
Similarly, a POST request coming to
the REST API end point on the server means that
the POST request brings in some data in the body of the message.
So, this information needs to be processed in the Express server
and the information that needs to be stored on
the database should be retrieved from the body of the incoming POST request and then
corresponding create request needs to
be created or initiated from the Express server to the MongoDB database,
and in the create request,
the information that has been retrieved from the body of
the POST request will be sent over to
the database to create a new document in a specific collection on the database.
And then the result of this operation will be sent
back to the client in the reply message.
So, any operation that is done on a REST API end point whether it is a GET,
a PUT, a POST,
or a DELETE operation,
as you see from these two examples,
will initiate a corresponding database operation behind the scenes.
So having understood this interaction,
what we realize is that an HTTP request coming in to
a REST API end point has to be mapped into a corresponding database operation.
So every incoming request, the GET, PUT, POST,
or DELETE means that a specific resource on the database may be accessed,
may be retrieved or a group of resources may be
retrieved from the database and then sent back to the server,
or a resource may be modified in response to a PUT or a POST
or even a DELETE request coming in to the REST API server.
So it is up to the Express server logic,
the business logic implemented in the Express REST API server,
to handle this translation of the incoming request whether it is a GET,
PUT, POST, or DELETE request into the corresponding database operation.
So let's look at an example of this in a little more detail.
So, coming to the combination of the Express router plus
MongoDB plus Mongoose acting as the ODM in between,
the operations to be performed of the database have to be initiated
inside the router that we built for each of the REST API end point.
So within the router,
even the GET method,
the PUT or the POST method.
The corresponding action to be performed on the database whether it is a GET request
causing a dishes find method to be executed or
a POST request causing a dishes create method to be executed,
will have to be done by our Express server resulting in
the corresponding operation being initiated on the MongoDB database.
So with this understanding of
how the requests are translated into a corresponding database operations,
let's proceed on to the two exercises where we will look at handling the GET, PUT, POST,
and DELETE request coming to the /dishes/:dishId end points and
also to modify specific comments that are in
the sub-documents enclosed inside the dish document.