Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,220 --> 00:00:03,030
Let's now improve the nested
2
00:00:03,030 --> 00:00:06,620
route implementation that we coded in the last lecture.
3
00:00:06,620 --> 00:00:07,500
And for that,
4
00:00:07,500 --> 00:00:11,213
we're gonna use a special advanced express feature.
5
00:00:12,840 --> 00:00:15,070
So, as you remember, in the last video
6
00:00:15,070 --> 00:00:18,920
we implemented a simple nested post route,
7
00:00:18,920 --> 00:00:21,860
so just this one here.
8
00:00:21,860 --> 00:00:25,240
Right. And so this means that the review route
9
00:00:25,240 --> 00:00:27,590
is kind of within the tour route.
10
00:00:27,590 --> 00:00:32,130
And again, because reviews belong to tours in a sense.
11
00:00:32,130 --> 00:00:32,963
Right?
12
00:00:32,963 --> 00:00:36,920
And so this is a very common thing to do in Api design.
13
00:00:36,920 --> 00:00:39,950
Now, the problem with this implementation is that it is,
14
00:00:39,950 --> 00:00:41,800
of course, a bit messy.
15
00:00:41,800 --> 00:00:45,420
And that is because we put a route for creating a review
16
00:00:45,420 --> 00:00:47,090
in the tour router.
17
00:00:47,090 --> 00:00:50,430
Simply because a route starts with slash tour.
18
00:00:50,430 --> 00:00:51,950
So that's a bit confusing,
19
00:00:51,950 --> 00:00:53,500
and what's also confusing
20
00:00:53,500 --> 00:00:56,480
is that we have something very similar to this here,
21
00:00:56,480 --> 00:00:59,160
also in our review route.
22
00:00:59,160 --> 00:01:01,203
So, basically this here.
23
00:01:01,203 --> 00:01:04,840
So when we create a new review without the nested route,
24
00:01:04,840 --> 00:01:08,450
this piece of the code here is actually exactly the same
25
00:01:08,450 --> 00:01:10,600
as this one. Right?
26
00:01:10,600 --> 00:01:12,940
And so, besides this being a bit confusing,
27
00:01:12,940 --> 00:01:16,020
we also have duplicate code which we would have to maintain
28
00:01:16,020 --> 00:01:19,910
in two separate places in case we wanted to change anything.
29
00:01:19,910 --> 00:01:23,120
And so again, that's a very bad practice.
30
00:01:23,120 --> 00:01:26,600
And so let's now fix this using an advanced express feature
31
00:01:26,600 --> 00:01:28,410
called mergeParams.
32
00:01:28,410 --> 00:01:31,270
So first off let's remove this code here
33
00:01:31,270 --> 00:01:34,690
which doesn't really belong to the tour router.
34
00:01:34,690 --> 00:01:38,500
Next up, we will actually import the review router
35
00:01:38,500 --> 00:01:40,840
into this tour router.
36
00:01:40,840 --> 00:01:41,740
Okay?
37
00:01:41,740 --> 00:01:44,460
So right now we are importing the review controller,
38
00:01:44,460 --> 00:01:47,330
but actually we want the review router.
39
00:01:47,330 --> 00:01:49,220
So let's get rid of this one,
40
00:01:49,220 --> 00:01:51,963
and actually I'm going to delete it altogether.
41
00:01:53,090 --> 00:01:53,923
And now,
42
00:01:54,960 --> 00:01:55,880
the review
43
00:01:57,070 --> 00:01:57,923
router.
44
00:02:05,500 --> 00:02:06,333
And okay.
45
00:02:07,410 --> 00:02:12,100
So, let's actually get this code
46
00:02:12,100 --> 00:02:13,480
and put it here at the top,
47
00:02:13,480 --> 00:02:15,903
just so that we see what we're doing,
48
00:02:17,140 --> 00:02:19,730
because we will actually create some new code
49
00:02:19,730 --> 00:02:21,620
right here at the top.
50
00:02:21,620 --> 00:02:23,560
And so what we're gonna do here
51
00:02:23,560 --> 00:02:24,393
is to say
52
00:02:26,781 --> 00:02:29,270
router.use.
53
00:02:29,270 --> 00:02:32,060
And so we will basically say that this tour router
54
00:02:32,060 --> 00:02:33,890
should use the review router
55
00:02:33,890 --> 00:02:37,373
in case it ever encounters a route like this.
56
00:02:43,070 --> 00:02:43,903
And
57
00:02:46,440 --> 00:02:48,670
Review routes.
58
00:02:48,670 --> 00:02:50,600
Did I call it routes or router?
59
00:02:50,600 --> 00:02:52,083
Should be called router.
60
00:02:53,630 --> 00:02:54,463
Okay?
61
00:02:55,690 --> 00:02:58,010
So, keep in mind that a router itself
62
00:02:58,010 --> 00:02:59,730
is really just a middleware.
63
00:02:59,730 --> 00:03:02,580
And so we can use the use method on it,
64
00:03:02,580 --> 00:03:05,200
and then say that for this specific route here,
65
00:03:05,200 --> 00:03:08,210
we want to use the review router instead.
66
00:03:08,210 --> 00:03:09,043
Okay?
67
00:03:09,043 --> 00:03:11,890
And so this is actually again mounting a router.
68
00:03:11,890 --> 00:03:14,300
So, if you take a look at that,
69
00:03:14,300 --> 00:03:15,870
here in the app.js,
70
00:03:15,870 --> 00:03:18,140
that's actually exactly what we did.
71
00:03:18,140 --> 00:03:20,410
So we did .use,
72
00:03:20,410 --> 00:03:22,090
then the URL,
73
00:03:22,090 --> 00:03:23,430
and then the router.
74
00:03:23,430 --> 00:03:25,040
And so here, what we're doing
75
00:03:25,040 --> 00:03:27,340
is actually the exact same thing.
76
00:03:27,340 --> 00:03:30,360
So this is the tour router, and so then we say
77
00:03:30,360 --> 00:03:33,250
well whenever you find a URL like this,
78
00:03:33,250 --> 00:03:36,010
well, then just use the review router.
79
00:03:36,010 --> 00:03:40,010
And so, right now, when we have a URL like this,
80
00:03:40,010 --> 00:03:44,700
it will start by getting into the tour router here,
81
00:03:44,700 --> 00:03:47,963
okay, because, again it starts with slash tours,
82
00:03:48,920 --> 00:03:49,753
okay,
83
00:03:49,753 --> 00:03:52,920
so basically it's rerouted into the tour router,
84
00:03:52,920 --> 00:03:55,400
then when it reaches the tour router,
85
00:03:55,400 --> 00:03:57,800
then it will match this URL here,
86
00:03:57,800 --> 00:04:02,370
and then it will again be rerouted into the review router.
87
00:04:02,370 --> 00:04:04,300
And like this, we have the tour router
88
00:04:04,300 --> 00:04:06,780
and the review router nicely separated
89
00:04:06,780 --> 00:04:09,300
and decoupled from one another.
90
00:04:09,300 --> 00:04:12,370
But now, there's actually still one piece missing
91
00:04:12,370 --> 00:04:15,170
because right now this review router here
92
00:04:15,170 --> 00:04:19,410
doesn't get access to this tour id parameter, okay.
93
00:04:19,410 --> 00:04:22,050
And so now we need to enable the review router
94
00:04:22,050 --> 00:04:26,200
to actually get access to this parameter here as well.
95
00:04:26,200 --> 00:04:29,980
So let's now move to the review router, okay.
96
00:04:29,980 --> 00:04:32,180
And so this is where the medical mergeParams
97
00:04:33,264 --> 00:04:34,810
that I mentioned right in the beginning
98
00:04:34,810 --> 00:04:35,943
comes into play.
99
00:04:38,030 --> 00:04:40,790
So here, in the express.router function,
100
00:04:40,790 --> 00:04:43,010
we can specify some options,
101
00:04:43,010 --> 00:04:45,760
and here all we need to do is set mergeParams
102
00:04:48,160 --> 00:04:49,170
to true.
103
00:04:49,170 --> 00:04:50,440
And that's it.
104
00:04:50,440 --> 00:04:53,750
But why do we actually need this here?
105
00:04:53,750 --> 00:04:55,780
Well, it's because, by default,
106
00:04:55,780 --> 00:04:57,850
each router only have access
107
00:04:57,850 --> 00:05:01,780
to the parameters of their specific routes, right.
108
00:05:01,780 --> 00:05:06,720
But here, in this route, so in this URL for this post,
109
00:05:06,720 --> 00:05:09,930
there's of course actually no tour id.
110
00:05:09,930 --> 00:05:12,630
But, we still want to get access to the tour id
111
00:05:12,630 --> 00:05:16,050
that was in this other router, right.
112
00:05:16,050 --> 00:05:17,430
So this here.
113
00:05:17,430 --> 00:05:20,420
And so, in order to get access to that parameter
114
00:05:20,420 --> 00:05:21,910
in this other router,
115
00:05:21,910 --> 00:05:25,570
we need to physically merge the parameters, okay.
116
00:05:25,570 --> 00:05:29,270
And so that's what mergeParams, set to true, does.
117
00:05:29,270 --> 00:05:32,663
And so now, no matter if we get a route like this,
118
00:05:34,310 --> 00:05:36,853
so let's just copy it here,
119
00:05:38,270 --> 00:05:41,383
Again it's easier to visualize it with an example.
120
00:05:42,280 --> 00:05:45,647
And so again, no matter if we get a route like this,
121
00:05:48,380 --> 00:05:49,840
or a route like this,
122
00:05:49,840 --> 00:05:54,530
it will now all end up in this handler here, okay.
123
00:05:54,530 --> 00:05:58,130
So in the end basically to this handler function.
124
00:05:58,130 --> 00:06:01,060
And again that works because all of the routes
125
00:06:01,060 --> 00:06:03,710
starting with this kind of pattern here
126
00:06:03,710 --> 00:06:06,720
will be redirected to this router
127
00:06:06,720 --> 00:06:09,960
exactly in this line of code here, okay.
128
00:06:09,960 --> 00:06:13,220
So we redirected to the review router here
129
00:06:13,220 --> 00:06:17,600
and so then in there it will match to this exact route,
130
00:06:17,600 --> 00:06:18,433
okay.
131
00:06:18,433 --> 00:06:19,990
And thanks to merge parameters,
132
00:06:19,990 --> 00:06:22,310
we then get access to this id
133
00:06:22,310 --> 00:06:25,700
which actually comes from the other router before.
134
00:06:25,700 --> 00:06:26,940
Great.
135
00:06:26,940 --> 00:06:28,843
So let's just do some clean up here,
136
00:06:29,870 --> 00:06:31,323
actually get rid of this,
137
00:06:32,260 --> 00:06:33,143
this here,
138
00:06:34,230 --> 00:06:35,063
and
139
00:06:36,310 --> 00:06:38,560
let's quickly try it out now.
140
00:06:38,560 --> 00:06:40,940
So I'm gonna create a new review here
141
00:06:42,100 --> 00:06:44,143
and let's try another tool this time.
142
00:06:47,180 --> 00:06:48,533
So the city wanderer.
143
00:06:51,160 --> 00:06:52,990
And all we need to do is to change
144
00:06:52,990 --> 00:06:57,030
that id right here in the URL, right.
145
00:06:57,030 --> 00:07:00,250
So, the rating is gonna be three,
146
00:07:00,250 --> 00:07:01,083
and then
147
00:07:03,570 --> 00:07:07,190
was kinda okay.
148
00:07:07,190 --> 00:07:10,943
All right, and so this review will still be done with Jonas,
149
00:07:11,960 --> 00:07:14,940
so from this account, okay,
150
00:07:14,940 --> 00:07:16,300
because that's still one
151
00:07:16,300 --> 00:07:20,890
that is logged in at this current moment, okay.
152
00:07:20,890 --> 00:07:21,963
So, let's send that,
153
00:07:23,160 --> 00:07:26,900
and indeed here we get the tour that we just specified,
154
00:07:26,900 --> 00:07:28,383
and still the same user.
155
00:07:30,110 --> 00:07:34,710
So that's again just to make sure check the tour
156
00:07:34,710 --> 00:07:37,110
to see if it now gets this review
157
00:07:37,110 --> 00:07:39,500
and yeah, it did.
158
00:07:39,500 --> 00:07:42,710
And so that merge parameter trick, that we just implemented,
159
00:07:42,710 --> 00:07:44,650
actually works just fine,
160
00:07:44,650 --> 00:07:46,280
and as intended.
161
00:07:46,280 --> 00:07:48,790
Now to see this in action, even better,
162
00:07:48,790 --> 00:07:50,560
we're gonna adapt the henry function
163
00:07:50,560 --> 00:07:53,420
for getting all the reviews for a specific tour
164
00:07:53,420 --> 00:07:54,793
right in the next video.
12063
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.