Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,437 --> 00:00:03,800
(Man) So in this lecture, you're gonna learn how to
2
00:00:03,800 --> 00:00:07,093
chain multiple middleware functions for the same route.
3
00:00:08,490 --> 00:00:11,500
So up until this point, whenever we wanted to define
4
00:00:11,500 --> 00:00:15,610
a middleware, we only ever passed one middleware function.
5
00:00:15,610 --> 00:00:19,510
So for example here, for handling this post request,
6
00:00:19,510 --> 00:00:22,700
well we only passed in this middleware function
7
00:00:22,700 --> 00:00:24,930
which is our createTour handler.
8
00:00:24,930 --> 00:00:27,400
Okay? And so that's the only function that is gonna
9
00:00:27,400 --> 00:00:30,880
be called whenever we get a post request.
10
00:00:30,880 --> 00:00:33,840
Right? But lets now say that we want to actually
11
00:00:33,840 --> 00:00:35,940
run multiple middleware functions.
12
00:00:35,940 --> 00:00:39,280
Now you might ask "Why would we want to do that?".
13
00:00:39,280 --> 00:00:42,713
Well we might, for example, run a middleware before
14
00:00:42,713 --> 00:00:45,760
createTour here to actually check the data that is
15
00:00:45,760 --> 00:00:47,060
coming in the body.
16
00:00:47,060 --> 00:00:50,490
Right? So a bit similar to what we had before.
17
00:00:50,490 --> 00:00:53,670
So we did this check ID middleware before in order to
18
00:00:53,670 --> 00:00:57,240
check if the ID is actually valid and doing so outside
19
00:00:57,240 --> 00:00:59,430
of the actual route handlers so that they are
20
00:00:59,430 --> 00:01:03,140
only concerned with getting, updating, or deleting
21
00:01:03,140 --> 00:01:04,080
a tour.
22
00:01:04,080 --> 00:01:07,340
Right? And so here, in this specific example with post, we
23
00:01:07,340 --> 00:01:09,130
might want to do the same thing.
24
00:01:09,130 --> 00:01:12,900
So as I said, we might want to check if request.buddy
25
00:01:12,900 --> 00:01:16,080
actually contains the data that we want for the tour.
26
00:01:16,080 --> 00:01:18,440
Okay? And so lets do that.
27
00:01:18,440 --> 00:01:22,020
And actually, I want you to first do it on your own.
28
00:01:22,020 --> 00:01:25,240
So this is kind of a challenge that I want you to complete
29
00:01:25,240 --> 00:01:27,670
in order to use the concepts that we've talked about
30
00:01:27,670 --> 00:01:30,450
so many times at this point and create your own
31
00:01:30,450 --> 00:01:31,450
middleware function.
32
00:01:32,470 --> 00:01:34,950
So let me just write it here as a comment.
33
00:01:34,950 --> 00:01:36,160
So I want you to create
34
00:01:39,370 --> 00:01:43,060
a checkBody middleware function
35
00:01:43,060 --> 00:01:44,660
and in there I want you to check
36
00:01:47,620 --> 00:01:52,620
if the body contains the name property for the tour name
37
00:01:52,670 --> 00:01:54,770
and the price property.
38
00:01:54,770 --> 00:01:58,020
All right? And if not, I want you to send back a
39
00:01:58,020 --> 00:01:59,590
four-hundred status code.
40
00:01:59,590 --> 00:02:04,000
If not, send back four-hundred, which stands for
41
00:02:04,000 --> 00:02:05,630
bad request.
42
00:02:05,630 --> 00:02:09,190
So basically, an invalid request from the client, which is
43
00:02:09,190 --> 00:02:13,060
in this case, trying to create a new tour without a name
44
00:02:13,060 --> 00:02:15,090
and without a price property.
45
00:02:15,090 --> 00:02:16,693
Okay! And that's actually it.
46
00:02:19,580 --> 00:02:20,640
And (laughs) actually not.
47
00:02:20,640 --> 00:02:25,640
So I also want you to add it to the post handler stack.
48
00:02:28,600 --> 00:02:29,900
Basically. Okay?
49
00:02:29,900 --> 00:02:31,700
So I want you to add it in here.
50
00:02:31,700 --> 00:02:33,430
And I will actually tell you how to do that
51
00:02:33,430 --> 00:02:35,730
because we didn't do it at this point.
52
00:02:35,730 --> 00:02:40,690
So its very easy. Lets say our function is called middleware
53
00:02:40,690 --> 00:02:43,590
and so what you have to do is to simply add that function
54
00:02:43,590 --> 00:02:46,800
here before the createTour handler that will
55
00:02:46,800 --> 00:02:49,180
ultimately create the tour. Okay?
56
00:02:49,180 --> 00:02:53,630
So this way when we have a post request for this route,
57
00:02:53,630 --> 00:02:57,334
it will then run this middleware first and only then
58
00:02:57,334 --> 00:02:59,280
the createTour.
59
00:02:59,280 --> 00:03:02,740
Okay? So that's how we chain two different middlewares.
60
00:03:02,740 --> 00:03:06,370
But now I want you to go ahead and uh create this middleware
61
00:03:06,370 --> 00:03:08,290
actually called checkBody.
62
00:03:08,290 --> 00:03:11,423
So pause the video now and please try to do it on your own.
63
00:03:15,220 --> 00:03:18,920
Okay! So I hope you were successful doing that and then you
64
00:03:18,920 --> 00:03:21,250
tested it and that everything works.
65
00:03:21,250 --> 00:03:24,323
So now let me go ahead and implement it in my way.
66
00:03:25,800 --> 00:03:28,210
So we have this error here but don't worry about it.
67
00:03:28,210 --> 00:03:31,083
Its because of this middleware that's not defined.
68
00:03:33,120 --> 00:03:36,553
And so let me do it here right after the checkID middleware.
69
00:03:38,440 --> 00:03:40,297
So exports.checkBody.
70
00:03:44,760 --> 00:03:47,503
We have to request response and next.
71
00:03:48,510 --> 00:03:51,560
And so its actually quite simple.
72
00:03:51,560 --> 00:03:56,030
So if there is no request.body.name,
73
00:03:57,050 --> 00:03:59,080
and I think that's the name but lets quickly go
74
00:03:59,080 --> 00:04:01,070
ahead and check that.
75
00:04:01,070 --> 00:04:03,570
So it is here is tour simple.
76
00:04:03,570 --> 00:04:05,340
Yeah, and indeed its called 'name'
77
00:04:05,340 --> 00:04:06,790
and the other one is 'price'.
78
00:04:09,810 --> 00:04:13,420
So if there is no request body name or
79
00:04:13,420 --> 00:04:17,560
no request body.price
80
00:04:20,440 --> 00:04:24,460
well, in this case, we will, just as before, return from
81
00:04:24,460 --> 00:04:27,163
this function and send back a response.
82
00:04:28,500 --> 00:04:33,500
So the status here is remember: four-hundred add then
83
00:04:33,610 --> 00:04:37,283
some 'json' where the status is fail.
84
00:04:38,520 --> 00:04:41,560
So that's always when we have a four-hundred code.
85
00:04:41,560 --> 00:04:42,750
Add then the message
86
00:04:45,097 --> 00:04:46,180
"Missing name
87
00:04:47,910 --> 00:04:49,400
or price".
88
00:04:49,400 --> 00:04:53,240
Okay? So price and name need to be there at the same time.
89
00:04:53,240 --> 00:04:57,500
So if either the price is not there or the name is not there
90
00:04:57,500 --> 00:04:59,993
then we already send this error message.
91
00:05:00,830 --> 00:05:04,500
Okay? But if everything is correct, well then we want
92
00:05:04,500 --> 00:05:06,283
to move on to the next middleware.
93
00:05:07,864 --> 00:05:09,110
And so that's what we say here.
94
00:05:09,110 --> 00:05:12,650
And then next middleware will of course be createTour.
95
00:05:14,833 --> 00:05:15,666
So this one.
96
00:05:17,260 --> 00:05:20,750
So, lets go ahead and then add it here.
97
00:05:20,750 --> 00:05:22,150
So tourController.checkBody.
98
00:05:25,090 --> 00:05:29,180
All right so now the error is gone and we should be able
99
00:05:29,180 --> 00:05:33,562
to test it now. So lets close the ones we're not interested
100
00:05:33,562 --> 00:05:38,000
in and create a new tour.
101
00:05:38,000 --> 00:05:40,890
So right now we have our body with the name, duration, and
102
00:05:40,890 --> 00:05:43,080
difficulty but there is no price.
103
00:05:43,080 --> 00:05:46,160
So we should now get our error. Right?
104
00:05:46,160 --> 00:05:50,150
So lets test that. Send. And indeed, we have 'Missing name
105
00:05:50,150 --> 00:05:53,630
or price' with our four-hundred bad request.
106
00:05:53,630 --> 00:05:57,090
Okay? And so lets analyze what happened here again.
107
00:05:57,090 --> 00:06:00,830
So our request hit this route here and it was a post
108
00:06:00,830 --> 00:06:05,360
request. And so, it then run this middleware function
109
00:06:05,360 --> 00:06:09,980
and from this function, we then send out this uh response
110
00:06:10,830 --> 00:06:15,380
saying that, uh, well the name or the price is missing.
111
00:06:15,380 --> 00:06:17,730
Okay? But if now add the price...
112
00:06:21,530 --> 00:06:23,730
...so it doesn't really matter what price,
113
00:06:23,730 --> 00:06:26,940
send it, well then everything works just fine
114
00:06:26,940 --> 00:06:29,083
and we get a 201 created.
115
00:06:30,480 --> 00:06:34,380
So what happened this time? Well, it checked for the body
116
00:06:34,380 --> 00:06:37,650
and this condition was not true and so it called the next
117
00:06:37,650 --> 00:06:41,420
middleware in the stack which is createTour.
118
00:06:41,420 --> 00:06:44,540
So it created a tour and sent back the result
119
00:06:44,540 --> 00:06:47,790
and like this, finished the request-response cycle.
120
00:06:47,790 --> 00:06:52,790
Right? So I hope that was a, uh, nice small challenge and
121
00:06:52,930 --> 00:06:56,340
we will be using this kind of logic here all the time
122
00:06:56,340 --> 00:06:59,560
where we chain multiple kind of handlers here for the
123
00:06:59,560 --> 00:07:02,760
same route. So for example here we could check if a
124
00:07:02,760 --> 00:07:06,540
certain user is logged in or if he has the privileges, so
125
00:07:06,540 --> 00:07:10,320
the access rights, to even write a new tour.
126
00:07:10,320 --> 00:07:13,300
Or really all kinds of stuff that we want to happen
127
00:07:13,300 --> 00:07:15,940
before the tour is actually created.
128
00:07:15,940 --> 00:07:19,100
Okay? And once again we do that because we want to take
129
00:07:19,100 --> 00:07:22,360
all the logic that is not really concerned with creating the
130
00:07:22,360 --> 00:07:25,860
new resource outside of that handler. So that that handler
131
00:07:25,860 --> 00:07:27,890
is only concerned really with the work
132
00:07:27,890 --> 00:07:29,040
that is supposed to do.
10821
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.