1
00:00:03,940 --> 00:00:08,444
In this module in the very first lesson,

2
00:00:08,444 --> 00:00:14,115
we learned how to build a full-fledged REST API server using express.

3
00:00:14,115 --> 00:00:17,120
So we're able to service the GET, PUT, POST,

4
00:00:17,120 --> 00:00:22,185
and DELETE requests coming to the various REST API end points.

5
00:00:22,185 --> 00:00:24,975
But, the server itself was simply returning

6
00:00:24,975 --> 00:00:29,115
a simple message in response to these requests.

7
00:00:29,115 --> 00:00:32,778
In a real REST API server,

8
00:00:32,778 --> 00:00:39,620
any incoming request will entail a corresponding operation to be performed on the back in

9
00:00:39,620 --> 00:00:46,610
the database perhaps to retrieve data to respond to a GET request,

10
00:00:46,610 --> 00:00:52,430
or maybe to modify data that exists on the server in response to a PUT request.

11
00:00:52,430 --> 00:00:56,270
Now in the rest of this module,

12
00:00:56,270 --> 00:01:03,465
we have studied about how we can interact from a node application with a MongoDB server,

13
00:01:03,465 --> 00:01:10,500
be it using the MongoDB driver or using Mongoose.

14
00:01:10,500 --> 00:01:15,600
Now, a full-fledged REST API server that is able to handle

15
00:01:15,600 --> 00:01:21,468
the request end to end will be possible only when we combine the two together.

16
00:01:21,468 --> 00:01:24,840
That is, an express-based server that does

17
00:01:24,840 --> 00:01:29,340
all the business logic processing and at the same time will issue

18
00:01:29,340 --> 00:01:34,200
the database requests to the MongoDB

19
00:01:34,200 --> 00:01:40,790
using the node MongoDB driver or using Mongoose.

20
00:01:40,790 --> 00:01:42,780
So how do we combine the two together?

21
00:01:42,780 --> 00:01:47,040
So this is what we will look at in this particular lesson,

22
00:01:47,040 --> 00:01:51,230
and the two exercises that we will do as part of this lesson.

23
00:01:51,230 --> 00:01:55,875
We now have learnt how to build

24
00:01:55,875 --> 00:01:59,400
a REST API server using Express

25
00:01:59,400 --> 00:02:03,950
and service the various requests coming to the REST API end points.

26
00:02:03,950 --> 00:02:10,765
We have also seen how we can interact with the database from our node application.

27
00:02:10,765 --> 00:02:17,280
Now, given that you have a GET request coming into the server as an example,

28
00:02:17,280 --> 00:02:20,550
to handle this GET request end to end,

29
00:02:20,550 --> 00:02:25,500
a GET request coming from the client means that the client wants to retrieve

30
00:02:25,500 --> 00:02:31,830
data from the server and use that data.

31
00:02:31,830 --> 00:02:34,590
So, a GET request coming into the server will have to be

32
00:02:34,590 --> 00:02:39,060
handled through the various processing done by, for example,

33
00:02:39,060 --> 00:02:43,880
the Express server and once the processing is done then

34
00:02:43,880 --> 00:02:47,340
Express server's business logic realizes that it needs to

35
00:02:47,340 --> 00:02:51,166
perform a query operation on the database.

36
00:02:51,166 --> 00:02:54,630
So, this may initiate a query to the database in order to

37
00:02:54,630 --> 00:02:58,605
fetch a set of documents from the database,

38
00:02:58,605 --> 00:03:02,850
and then the retrieved data will then be

39
00:03:02,850 --> 00:03:07,650
transformed into a reply message and then sent back to the server.

40
00:03:07,650 --> 00:03:15,340
So this end-to-end handling of the request and response involves two parts.

41
00:03:15,340 --> 00:03:18,060
One of course doing the business logic in

42
00:03:18,060 --> 00:03:23,070
the Express server and then doing the interaction with

43
00:03:23,070 --> 00:03:27,270
the database from the node application from Express server which is

44
00:03:27,270 --> 00:03:32,580
a node application using either MongoDB driver or Mongoose.

45
00:03:32,580 --> 00:03:36,190
We will be using Mongoose in the exercises.

46
00:03:36,190 --> 00:03:42,005
Similarly, a POST request coming to

47
00:03:42,005 --> 00:03:46,420
the REST API end point on the server means that

48
00:03:46,420 --> 00:03:51,330
the POST request brings in some data in the body of the message.

49
00:03:51,330 --> 00:03:55,795
So, this information needs to be processed in the Express server

50
00:03:55,795 --> 00:04:01,150
and the information that needs to be stored on

51
00:04:01,150 --> 00:04:07,208
the database should be retrieved from the body of the incoming POST request and then

52
00:04:07,208 --> 00:04:11,380
corresponding create request needs to

53
00:04:11,380 --> 00:04:17,590
be created or initiated from the Express server to the MongoDB database,

54
00:04:17,590 --> 00:04:19,450
and in the create request,

55
00:04:19,450 --> 00:04:22,330
the information that has been retrieved from the body of

56
00:04:22,330 --> 00:04:25,870
the POST request will be sent over to

57
00:04:25,870 --> 00:04:33,130
the database to create a new document in a specific collection on the database.

58
00:04:33,130 --> 00:04:36,550
And then the result of this operation will be sent

59
00:04:36,550 --> 00:04:40,200
back to the client in the reply message.

60
00:04:40,200 --> 00:04:45,640
So, any operation that is done on a REST API end point whether it is a GET,

61
00:04:45,640 --> 00:04:47,155
a PUT, a POST,

62
00:04:47,155 --> 00:04:48,951
or a DELETE operation,

63
00:04:48,951 --> 00:04:51,005
as you see from these two examples,

64
00:04:51,005 --> 00:04:58,063
will initiate a corresponding database operation behind the scenes.

65
00:04:58,063 --> 00:05:01,120
So having understood this interaction,

66
00:05:01,120 --> 00:05:05,590
what we realize is that an HTTP request coming in to

67
00:05:05,590 --> 00:05:10,360
a REST API end point has to be mapped into a corresponding database operation.

68
00:05:10,360 --> 00:05:12,260
So every incoming request, the GET, PUT, POST,

69
00:05:12,260 --> 00:05:19,270
or DELETE means that a specific resource on the database may be accessed,

70
00:05:19,270 --> 00:05:23,530
may be retrieved or a group of resources may be

71
00:05:23,530 --> 00:05:28,360
retrieved from the database and then sent back to the server,

72
00:05:28,360 --> 00:05:34,015
or a resource may be modified in response to a PUT or a POST

73
00:05:34,015 --> 00:05:40,425
or even a DELETE request coming in to the REST API server.

74
00:05:40,425 --> 00:05:44,170
So it is up to the Express server logic,

75
00:05:44,170 --> 00:05:47,545
the business logic implemented in the Express REST API server,

76
00:05:47,545 --> 00:05:53,860
to handle this translation of the incoming request whether it is a GET,

77
00:05:53,860 --> 00:05:58,765
PUT, POST, or DELETE request into the corresponding database operation.

78
00:05:58,765 --> 00:06:03,085
So let's look at an example of this in a little more detail.

79
00:06:03,085 --> 00:06:07,480
So, coming to the combination of the Express router plus

80
00:06:07,480 --> 00:06:12,445
MongoDB plus Mongoose acting as the ODM in between,

81
00:06:12,445 --> 00:06:17,710
the operations to be performed of the database have to be initiated

82
00:06:17,710 --> 00:06:23,275
inside the router that we built for each of the REST API end point.

83
00:06:23,275 --> 00:06:24,310
So within the router,

84
00:06:24,310 --> 00:06:26,740
even the GET method,

85
00:06:26,740 --> 00:06:28,615
the PUT or the POST method.

86
00:06:28,615 --> 00:06:35,440
The corresponding action to be performed on the database whether it is a GET request

87
00:06:35,440 --> 00:06:39,940
causing a dishes find method to be executed or

88
00:06:39,940 --> 00:06:44,235
a POST request causing a dishes create method to be executed,

89
00:06:44,235 --> 00:06:48,595
will have to be done by our Express server resulting in

90
00:06:48,595 --> 00:06:53,285
the corresponding operation being initiated on the MongoDB database.

91
00:06:53,285 --> 00:06:55,555
So with this understanding of

92
00:06:55,555 --> 00:07:00,655
how the requests are translated into a corresponding database operations,

93
00:07:00,655 --> 00:07:06,720
let's proceed on to the two exercises where we will look at handling the GET, PUT, POST,

94
00:07:06,720 --> 00:07:15,040
and DELETE request coming to the /dishes/:dishId end points and

95
00:07:15,040 --> 00:07:18,940
also to modify specific comments that are in

96
00:07:18,940 --> 00:07:24,470
the sub-documents enclosed inside the dish document.