﻿1
00:00:01,330 --> 00:00:02,860
‫Another nice small feature

2
00:00:02,860 --> 00:00:04,910
‫that we can add to an API is

3
00:00:04,910 --> 00:00:08,070
‫to provide an alias route to a request

4
00:00:08,070 --> 00:00:09,660
‫that might be very popular,

5
00:00:09,660 --> 00:00:11,963
‫so it might be requested all the time.

6
00:00:13,400 --> 00:00:15,260
‫And, for example, we might want

7
00:00:15,260 --> 00:00:17,790
‫to provide a route specifically

8
00:00:17,790 --> 00:00:20,203
‫for the five best cheap tours.

9
00:00:21,324 --> 00:00:23,530
‫So, if we'd use our regular route here

10
00:00:23,530 --> 00:00:25,530
‫with the filters and with all the features

11
00:00:25,530 --> 00:00:26,580
‫that we already have,

12
00:00:26,580 --> 00:00:29,930
‫the request would look a little bit like this.

13
00:00:29,930 --> 00:00:32,090
‫So, let's get ride of all of this.

14
00:00:32,090 --> 00:00:34,510
‫So, the five best and cheapest tours.

15
00:00:34,510 --> 00:00:37,340
‫So, that means we have a limit of five

16
00:00:39,495 --> 00:00:42,663
‫and then we're gonna sort by ratings and by price.

17
00:00:44,300 --> 00:00:47,640
‫So, sort and then the average rating,

18
00:00:47,640 --> 00:00:52,640
‫and descending, so minus ratings average.

19
00:00:54,110 --> 00:00:56,750
‫And, in case they have the same average,

20
00:00:56,750 --> 00:00:59,270
‫then we want the cheapest price possible.

21
00:00:59,270 --> 00:01:02,353
‫And so, we're gonna sort also by price.

22
00:01:03,610 --> 00:01:06,060
‫Let's quickly take a look at the results.

23
00:01:06,060 --> 00:01:08,163
‫And, indeed, we get five tours here.

24
00:01:09,670 --> 00:01:12,560
‫And, Postman does this scrolling again.

25
00:01:12,560 --> 00:01:14,517
‫So, we have tours with 4.9,

26
00:01:15,951 --> 00:01:18,640
‫4.9, 4.9 again,

27
00:01:18,640 --> 00:01:20,553
‫4.8, and 4.7.

28
00:01:22,250 --> 00:01:27,060
‫And so, these are actually our five best and cheapest tours.

29
00:01:27,060 --> 00:01:29,150
‫If we said cheapest and best, well,

30
00:01:29,150 --> 00:01:32,270
‫then maybe we would have to sort by price first

31
00:01:32,270 --> 00:01:34,580
‫because some of them are not really cheap.

32
00:01:34,580 --> 00:01:36,320
‫For example, this one.

33
00:01:36,320 --> 00:01:40,180
‫But, we selected, first, the average.

34
00:01:40,180 --> 00:01:43,160
‫And so, we sorted by the highest average first

35
00:01:43,160 --> 00:01:46,700
‫and, only then in case of a tie, basically,

36
00:01:46,700 --> 00:01:47,900
‫only then, by the price.

37
00:01:49,120 --> 00:01:51,340
‫Now, let's say that this is a request

38
00:01:51,340 --> 00:01:53,350
‫that is done all the time and we want

39
00:01:53,350 --> 00:01:55,060
‫to provide a route that is simple

40
00:01:55,060 --> 00:01:57,453
‫and easy to memorize for the user.

41
00:01:58,670 --> 00:02:02,110
‫So, let's go to our app and try to implement that.

42
00:02:02,110 --> 00:02:06,330
‫And, we're gonna start at their routes, so tour router.

43
00:02:06,330 --> 00:02:08,703
‫And, we then need to create a new route.

44
00:02:09,550 --> 00:02:12,390
‫And, let's do that here, right at the top.

45
00:02:12,390 --> 00:02:16,913
‫So, router.route.

46
00:02:18,650 --> 00:02:21,370
‫And so, how are we gonna call this route?

47
00:02:21,370 --> 00:02:25,293
‫Well, let's say top-5-cheap.

48
00:02:26,590 --> 00:02:30,010
‫So, quite a simple name, but it's gonna work.

49
00:02:30,010 --> 00:02:31,810
‫And then, we actually only want

50
00:02:31,810 --> 00:02:34,103
‫to get requests to this route.

51
00:02:35,170 --> 00:02:37,480
‫And so, now it's time to think.

52
00:02:37,480 --> 00:02:41,370
‫How are we gonna implement this functionality?

53
00:02:41,370 --> 00:02:43,530
‫Well, in essence, what we want is

54
00:02:43,530 --> 00:02:47,273
‫to actually still get all the tours just like here.

55
00:02:48,210 --> 00:02:51,070
‫And so, let me go ahead and start by copying

56
00:02:51,070 --> 00:02:52,930
‫and pasting that here.

57
00:02:52,930 --> 00:02:56,320
‫So, we really still want to use this function here

58
00:02:56,320 --> 00:02:57,740
‫because it doesn't really make sense

59
00:02:57,740 --> 00:03:00,900
‫to rewrite all of the logic that is in there.

60
00:03:00,900 --> 00:03:04,010
‫But, before we can call this route ender,

61
00:03:04,010 --> 00:03:06,690
‫we basically want to prefill some of the fields

62
00:03:06,690 --> 00:03:07,833
‫in the query string.

63
00:03:08,970 --> 00:03:10,340
‫So, we already know

64
00:03:10,340 --> 00:03:13,590
‫that our query string should look like this.

65
00:03:13,590 --> 00:03:16,370
‫And, actually we can add some more stuff here.

66
00:03:16,370 --> 00:03:18,070
‫But, in essence, our query string

67
00:03:18,070 --> 00:03:19,970
‫has to look something like this.

68
00:03:19,970 --> 00:03:23,100
‫And so, the solution is gonna be to run a middleware

69
00:03:23,100 --> 00:03:26,163
‫before we actually run this getAllTours handler.

70
00:03:27,080 --> 00:03:28,760
‫And so, that middleware function is then

71
00:03:28,760 --> 00:03:31,843
‫gonna manipulate the query object that's coming in.

72
00:03:33,010 --> 00:03:35,210
‫And so, this is yet another really nice example

73
00:03:35,210 --> 00:03:38,150
‫of using middleware because you really need

74
00:03:38,150 --> 00:03:41,100
‫to get familiar and used to this concept

75
00:03:41,100 --> 00:03:44,150
‫of using middleware strategically in order

76
00:03:44,150 --> 00:03:47,123
‫to change the request object as we need it.

77
00:03:48,690 --> 00:03:52,100
‫So, let's add it here then.

78
00:03:52,100 --> 00:03:53,920
‫So, tourController.

79
00:03:53,920 --> 00:03:55,150
‫And then, the middleware function

80
00:03:55,150 --> 00:03:57,630
‫that we're gonna create in a second,

81
00:03:57,630 --> 00:03:59,733
‫which is gonna be called aliasTopTours.

82
00:04:05,060 --> 00:04:08,820
‫So, let's copy that name, move to the tourController

83
00:04:08,820 --> 00:04:11,843
‫and I'm gonna put it here at the very top.

84
00:04:14,100 --> 00:04:15,217
‫So, exports.,

85
00:04:17,370 --> 00:04:19,823
‫then request,

86
00:04:21,650 --> 00:04:22,600
‫response,

87
00:04:22,600 --> 00:04:24,030
‫and what's the third argument

88
00:04:24,030 --> 00:04:27,180
‫that we need in the middleware, remember?

89
00:04:27,180 --> 00:04:30,700
‫And, I know the Express section is kind of far away already

90
00:04:30,700 --> 00:04:33,280
‫at this point, but I hope you remember

91
00:04:33,280 --> 00:04:36,230
‫that the third argument here needs to be next.

92
00:04:36,230 --> 00:04:38,970
‫So, in order to call the next middleware

93
00:04:38,970 --> 00:04:40,520
‫that's in the middleware stack.

94
00:04:45,600 --> 00:04:46,640
‫Give it a save.

95
00:04:46,640 --> 00:04:49,203
‫And so, this error there should disappear.

96
00:04:53,554 --> 00:04:55,950
‫And so, let's do what we said we would do.

97
00:04:55,950 --> 00:04:58,870
‫So, basically to manipulate the query object

98
00:04:58,870 --> 00:05:01,960
‫so that when it reaches the getAllTours handler,

99
00:05:01,960 --> 00:05:03,423
‫it's then already different.

100
00:05:04,270 --> 00:05:07,160
‫So, let's first do that and then really

101
00:05:07,160 --> 00:05:08,783
‫take a look at what happens.

102
00:05:10,010 --> 00:05:13,280
‫So, remember, the limit is gonna be set to five.

103
00:05:13,280 --> 00:05:15,320
‫And, remember that everything is strings here

104
00:05:15,320 --> 00:05:17,370
‫and so I'm setting this also to a string.

105
00:05:19,350 --> 00:05:20,703
‫Now, what else did we had?

106
00:05:21,580 --> 00:05:25,200
‫So, we have this sort, so let's just copy this here

107
00:05:26,400 --> 00:05:30,623
‫and say req.query.sort.

108
00:05:32,247 --> 00:05:35,050
‫It should be this.

109
00:05:35,050 --> 00:05:39,230
‫And then, we can actually also specify some fields

110
00:05:39,230 --> 00:05:42,560
‫so that the user doesn't get all of the fields

111
00:05:42,560 --> 00:05:43,660
‫that we have in there.

112
00:05:46,420 --> 00:05:49,033
‫So, let's say that we only want the name, the price,

113
00:05:50,070 --> 00:05:52,973
‫the ratings average, the summary,

114
00:05:54,570 --> 00:05:55,653
‫and the difficulty.

115
00:05:58,650 --> 00:06:02,600
‫Give it a save and now all we need is next,

116
00:06:02,600 --> 00:06:05,730
‫otherwise this middleware will be stuck here forever

117
00:06:05,730 --> 00:06:07,043
‫and cannot move on.

118
00:06:08,440 --> 00:06:10,420
‫So, let's now figure out a bit better

119
00:06:10,420 --> 00:06:12,690
‫what's actually gonna happen here.

120
00:06:12,690 --> 00:06:16,900
‫So, as soon as someone hits the top-5-cheap route,

121
00:06:16,900 --> 00:06:17,733
‫we will start.

122
00:06:17,733 --> 00:06:21,470
‫The first middleware that's gonna be run is aliasTopTours.

123
00:06:21,470 --> 00:06:23,433
‫So, the function that we just created.

124
00:06:25,420 --> 00:06:27,060
‫So, what this is gonna do is

125
00:06:27,060 --> 00:06:30,070
‫that it will set these properties of the query object

126
00:06:30,070 --> 00:06:32,490
‫to these values that we specified here.

127
00:06:32,490 --> 00:06:35,800
‫Basically prefilling parts of the query object

128
00:06:35,800 --> 00:06:39,453
‫before we then reach the getAllTours handler.

129
00:06:40,720 --> 00:06:43,670
‫And so, as soon as we then get to this function,

130
00:06:43,670 --> 00:06:46,080
‫the query object is already prefilled,

131
00:06:46,080 --> 00:06:47,960
‫even if the user didn't put any

132
00:06:47,960 --> 00:06:49,963
‫of these parameters in the query string.

133
00:06:51,230 --> 00:06:52,170
‫And so, in essence,

134
00:06:52,170 --> 00:06:55,137
‫what we're doing here is prefilling the query string

135
00:06:55,137 --> 00:06:57,650
‫for the user so that the user doesn't have

136
00:06:57,650 --> 00:06:59,083
‫to do it on his own.

137
00:07:01,024 --> 00:07:03,683
‫And so, let's now actually go ahead and try this.

138
00:07:04,780 --> 00:07:06,003
‫So, I'm copying this.

139
00:07:09,380 --> 00:07:11,090
‫And, here it's not tours.

140
00:07:11,090 --> 00:07:14,463
‫It is top-5-tours.

141
00:07:16,710 --> 00:07:18,620
‫Whoa, what's going on here?

142
00:07:18,620 --> 00:07:21,960
‫For some reason, it doesn't find this route,

143
00:07:21,960 --> 00:07:23,360
‫so let's go back.

144
00:07:23,360 --> 00:07:25,013
‫So, top-5-tours.

145
00:07:26,140 --> 00:07:28,720
‫And so, here in the route, ah, okay.

146
00:07:28,720 --> 00:07:30,690
‫It's called top-5-cheap,

147
00:07:30,690 --> 00:07:32,093
‫so my mistake here.

148
00:07:33,440 --> 00:07:35,780
‫So, cheap, send it now,

149
00:07:35,780 --> 00:07:38,123
‫and that's still not correct.

150
00:07:40,030 --> 00:07:41,110
‫Ah, yeah.

151
00:07:41,110 --> 00:07:46,110
‫Of course, I still have to put the tours here, of course.

152
00:07:46,140 --> 00:07:48,300
‫So, we are in the tours resource

153
00:07:48,300 --> 00:07:50,210
‫and the whole router is mounted

154
00:07:50,210 --> 00:07:53,670
‫onto this tours route, remember that.

155
00:07:53,670 --> 00:07:55,430
‫And so, it's in that tours,

156
00:07:55,430 --> 00:07:58,140
‫that kind of mini application, remember that,

157
00:07:58,140 --> 00:08:00,723
‫where I created this top-5-cheap route.

158
00:08:01,790 --> 00:08:03,470
‫So, let's hope it works now.

159
00:08:03,470 --> 00:08:04,703
‫And, indeed, it does.

160
00:08:07,350 --> 00:08:11,030
‫So, that worked, that's fantastic.

161
00:08:11,030 --> 00:08:15,090
‫Now, of course, we could do a lot more alias routes here,

162
00:08:15,090 --> 00:08:16,740
‫but that's not really necessary.

163
00:08:16,740 --> 00:08:19,400
‫I really just wanted to show you the concept,

164
00:08:19,400 --> 00:08:21,630
‫show you that it can be a nice feature

165
00:08:21,630 --> 00:08:25,380
‫to add to any API and also I wanted to, again,

166
00:08:25,380 --> 00:08:27,933
‫show you the power of middlewares.

167
00:08:29,160 --> 00:08:30,950
‫So, we're talking about Mongoose here,

168
00:08:30,950 --> 00:08:34,360
‫but still, it's very important to keep getting familiar

169
00:08:34,360 --> 00:08:37,513
‫with the Express concepts that we talked about before.

170
00:08:39,040 --> 00:08:42,120
‫Anyway, we're now done with our API features.

171
00:08:42,120 --> 00:08:44,950
‫In the next video, we're just gonna quickly refactor all

172
00:08:44,950 --> 00:08:48,690
‫of this to make the entire code a bit more modular

173
00:08:48,690 --> 00:08:50,293
‫and, of course, better.

