Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
In this video, let's create a special type 2
2
of middleware called param middleware. 3
3
So param middleware is middleware that only runs 4
4
for certain parameters, so basically, 5
5
when we have a certain parameter in our URL. 6
6
Now in our example here, the only parameter 7
7
that we might have in our route URL is the id, right? 8
8
And so we can write middleware that only runs 9
9
when this id is present in the URL, 10
10
okay, so let me show you how to do it. 11
11
It is quite simple actually, so it's on our router 12
12
and then the param method, okay. 13
13
And so here we specify first the parameter 14
14
that we actually want to search for, 15
15
so basically the parameter for which 16
16
this middleware is gonna run, and it's called id, 17
17
and then of course our actual middleware function. 18
18
And as usual, we have access to the request 19
19
and to the response object, and then 20
20
senses a middleware function also 21
21
to the next function, right? 22
22
Now in a param middleware function, 23
23
we actually get access to a fourth argument 24
24
and that one is the value of the parameter in question. 25
25
So we usually call that one val, which stands for value. 26
26
And so now we can go ahead and simply log it 27
27
to the console, just to take a look 28
28
if this actually works, so let's say Tour id is 29
29
and then id, right? 30
30
Next up, we also have to call next, right? 31
31
Because otherwise the request response cycle 32
32
will get stuck in this middleware function 33
33
and it's not gonna be able to move on 34
34
to the next middleware in the stack, right? 35
35
Which are gonna be these routes down here. 36
36
All right, so let's give it a save and check it out now. 37
37
And we want it to not for get all users 38
38
but for get one Tour, right? 39
39
Well let me show you first what happens 40
40
if we do not have any id, okay, and so now, 41
41
we don't see any log down in our console. 42
42
But if I now sent the same request on this route 43
43
where we have the id, let's see what happens then. 44
44
Oh, it tells me that id is not defined, 45
45
and indeed it's not id, so that was a stupid mistake. 46
46
So it is val for value, right? 47
47
So remember how I said that this value parameter 48
48
is the one that will actually hold 49
49
the value of the id parameter, and so of course 50
50
that's the one that we need to then use here 51
51
in order to get access to that id. 52
52
So let's try that again, and now indeed 53
53
we have Tour id is two, right? 54
54
So that log came right from this function here. 55
55
Now, what I also want to show you is that 56
56
this middleware function is not going to 57
57
run for any of the user routes. 58
58
So let's say we call a user with a specific id, 59
59
so let's go ahead and copy this one here, 60
60
create a new request with an id, 61
61
and let me actually save it also. 62
62
So get user and save to user, and when I send it here 63
63
we of course get our standard response of 64
64
this route is not defined, but you also see 65
65
that we have no log like we had before. 66
66
And so that is of course because this middleware function 67
67
is only specified in our tour router. 68
68
So in this kind of local mini application, 69
69
so again, that is the analogy that I like to make. 70
70
So basically that each router is 71
71
kind of a mini sub-application, one for each resource. 72
72
And so since this middleware is only specified 73
73
on this router, well then of course, 74
74
it is only part of the middleware stack 75
75
if we are actually inside of this sub-application. 76
76
Makes sense? 77
77
So let's suppose we have an incoming request on tours/id. 78
78
So that request will then go through all these middleware, 79
79
so first this middleware, then this one, 80
80
then this middleware, then this one, 81
81
so all of these are part of the middleware stack 82
82
and then it will finally hit this middleware 83
83
and since this is actually the route, 84
84
it will then get into tourRouter middleware. 85
85
Okay, and so from there it then goes right into 86
86
this middleware, and so then this code will be run. 87
87
And again, that's only because it has an id in the route. 88
88
If not, well, then this would simply be ignored 89
89
and it would move on right to the next middleware 90
90
in the stack, so these down here, right? 91
91
Cool, so that is how param middleware works; 92
92
but for now it is not really that useful. 93
93
But we can actually use it for 94
94
a very practical use case here. 95
95
So let's go to our handler functions here 96
96
and you see that in all of the handler functions 97
97
that actually use the id, we check if the id is valid. 98
98
So we do it here in get tour, and we also do it 99
99
in update tour, so here, and in delete tour. 100
100
So all these three functions have this very similar code 101
101
where they check if the id is valid 102
102
and if not, they send back this Invalid ID response. 103
103
So we have all this code in the same place 104
104
and as you already know, it is not a good practice 105
105
to repeat code and so what we can do here 106
106
is to use the concept of param middleware; 107
107
and perform this check here in an outside middleware 108
108
that it's gonna run before the request 109
109
even hits these handler functions. 110
110
So let's go ahead and copy or actually cut 111
111
the code from here and create a new middleware function 112
112
called checkID, and of course I also need to export that. 113
113
So checkID and we have access, request, response, 114
114
next, and again, keep in mind that 115
115
it is a param middleware and so the fourth argument 116
116
will be the value of the parameter. 117
117
Okay, paste it here, and then don't forget 118
118
to call next at the end of the middleware, all right? 119
119
And what's also very important is that 120
120
we have this return statement here, 121
121
because if we didn't have this return here, 122
122
well, then express would send this response back 123
123
but it would still continue running 124
124
the code in this function. 125
125
And so after sending the response, 126
126
it will then still hit this next function 127
127
and it would move on to the next middleware 128
128
and will then send another response to the client. 129
129
But that is really not allowed, 130
130
so remember that we actually run into this error before, 131
131
where it told us that we were not allowed 132
132
to send headers after the response had already been sent. 133
133
And so that's the kind of error 134
134
that we would run into if we didn't 135
135
have this return statement, okay. 136
136
So again, just make sure that after sending this response, 137
137
the function will return so that it will finish 138
138
and it will never call this next. 139
139
So never forget that, but of course 140
140
we're gonna do this multiple times 141
141
throughout the rest of the course 142
142
and so you will get used to this kind of pattern. 143
143
So let's go ahead and remove this repeated code 144
144
from all of these other functions 145
145
so here we have it again, and yeah. 146
146
I also want this code here just to make sure 147
147
that the function's actually running, 148
148
and okay, so now we can get rid of this here 149
149
and replace it with our newly created controller function. 150
150
So tourController, checkID, 151
151
so let's check that out now, 152
152
and well again, just to make sure 153
153
that it doesn't run where we 154
154
do not have an id, so we have no log here, 155
155
so everything is still working on that side. 156
156
And now get tour with a regular id, 157
157
so a valid id, and so let's see. 158
158
Well, we get the Tour id is log, 159
159
and so that means if it did actually run 160
160
our checkID middleware, right? 161
161
And if we now try an invalid id, 162
162
well then we get our Invalid ID message, 163
163
the 404 error code, and of course our tour id. 164
164
Let's do the same with the patch, 165
165
we already have an invalid ID here 166
166
and so it also runs on this one. 167
167
So, perfect, right? 168
168
So we no longer have the checkID code 169
169
in the update handler that we just invoked basically, 170
170
but still our ID is checked because 171
171
we have that middleware, so this here. 172
172
We have that middleware in the stack 173
173
before it actually hits the update tourController. 174
174
So this middleware is now part of our pipeline 175
175
as you can imagine it, now you might argue 176
176
that we might simply create a simple function 177
177
which could also check for the ID 178
178
and I call that function inside of 179
179
each of these tour function, and then call it inside 180
180
each of these relevant tour controllers; 181
181
but that would really go against 182
182
the philosophy of express, where we should always work 183
183
with the middleware stack, so with this pipeline 184
184
as much as we can, okay, and so these functions here, 185
185
they do not have to worry at all about validation. 186
186
Each of these functions has only one purpose 187
187
which is to do what they say, 188
188
so this one just gets the tour, 189
189
this one just creates a tour, this one just updates, 190
190
and this one just deletes, it doesn't check, 191
191
it doesn't have to worry about any of that. 192
192
And if we would now add another controller here 193
193
also depending on the id, well then that would automatically 194
194
also check if the id is invalid 195
195
without us having to do any additional steps. 196
196
So this automatically will check for the id 197
197
and so that's really handy and also how 198
198
express apps should work, great. 199
199
So we have another tool in our express toolbox that 200
200
we can now use in order to write our express applications.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.