All language subtitles for 008 Responding to URL Parameters_Downloadly.ir_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:01,140 --> 00:00:02,070 So in this lecture, 2 00:00:02,070 --> 00:00:03,570 you're gonna learn an easy way 3 00:00:03,570 --> 00:00:06,810 of defining parameters right in the URL, 4 00:00:06,810 --> 00:00:08,940 how to then read these parameters, 5 00:00:08,940 --> 00:00:11,323 and also, how to respond to them. 6 00:00:12,930 --> 00:00:16,230 Now, what we want to actually implement in this lecture 7 00:00:16,230 --> 00:00:19,260 is a way of getting only one tour. 8 00:00:19,260 --> 00:00:21,430 So right now, we have this endpoint here, 9 00:00:21,430 --> 00:00:23,060 which gives us all the tours, 10 00:00:23,060 --> 00:00:25,500 and we want to have something like this. 11 00:00:25,500 --> 00:00:28,710 So let's copy it into a new request for now, 12 00:00:28,710 --> 00:00:31,930 and then, we want slash, and then the ID of the tour. 13 00:00:31,930 --> 00:00:34,610 So for example, let's say five, okay? 14 00:00:34,610 --> 00:00:36,770 So, this is just like we talked about 15 00:00:36,770 --> 00:00:39,120 in the REST API lecture, 16 00:00:39,120 --> 00:00:44,120 where I said that if we hit this endpoint without any ID, 17 00:00:44,420 --> 00:00:46,350 so just like this here, 18 00:00:46,350 --> 00:00:48,490 well, then we would get all the tours. 19 00:00:48,490 --> 00:00:51,550 But if we would specify an ID after that, 20 00:00:51,550 --> 00:00:53,210 so, just like this, 21 00:00:53,210 --> 00:00:55,250 and of course it doesn't have to be an ID, 22 00:00:55,250 --> 00:00:57,600 it can be any unique identifier, 23 00:00:57,600 --> 00:01:00,080 but in this case, the easiest way of implementing it 24 00:01:00,080 --> 00:01:02,230 is to just use IDs, okay? 25 00:01:02,230 --> 00:01:05,349 So, this here is of course a variable, 26 00:01:05,349 --> 00:01:09,050 because it can be five, but it can also be anything else. 27 00:01:09,050 --> 00:01:11,700 And so this piece of the URL here, 28 00:01:11,700 --> 00:01:15,140 so this here, this is a variable, okay? 29 00:01:15,140 --> 00:01:16,390 And so what we need to do 30 00:01:16,390 --> 00:01:19,983 is to define a route which can accept a variable. 31 00:01:20,870 --> 00:01:23,600 All right, and so let's do that here, 32 00:01:23,600 --> 00:01:25,633 and actually right after this one. 33 00:01:26,740 --> 00:01:29,200 And I'm just gonna go ahead and copy it, 34 00:01:29,200 --> 00:01:32,143 because you already know how to write it at this point. 35 00:01:33,920 --> 00:01:35,360 And so, yeah. 36 00:01:35,360 --> 00:01:36,793 So here we have the route, 37 00:01:37,860 --> 00:01:41,410 and so now all we need to do is to add that variable. 38 00:01:41,410 --> 00:01:42,600 And that's very easy. 39 00:01:42,600 --> 00:01:45,370 So we have our slash again, and then the variable, 40 00:01:45,370 --> 00:01:48,580 we define it using a colon, like this. 41 00:01:48,580 --> 00:01:51,910 And so like this, we created a variable called ID. 42 00:01:51,910 --> 00:01:54,090 It could of course be anything else, 43 00:01:54,090 --> 00:01:56,280 like var, or X. 44 00:01:56,280 --> 00:01:57,510 Doesn't really matter. 45 00:01:57,510 --> 00:02:01,130 But this one is called ID, all right? 46 00:02:01,130 --> 00:02:03,653 Now we don't want to send back anything like this, 47 00:02:04,490 --> 00:02:06,500 so let's just comment it out. 48 00:02:06,500 --> 00:02:08,280 And what I want you to take a look at 49 00:02:08,280 --> 00:02:10,002 is at console dot log, 50 00:02:11,870 --> 00:02:14,170 req dot params. 51 00:02:14,170 --> 00:02:18,390 Okay, so request dot params is where all the parameters 52 00:02:18,390 --> 00:02:22,240 of all the variables that we define here are stored, okay? 53 00:02:22,240 --> 00:02:26,190 So these variables here in the URL are called parameters, 54 00:02:26,190 --> 00:02:28,960 and again, they are in req dot params, 55 00:02:28,960 --> 00:02:31,400 available for us to use now. 56 00:02:31,400 --> 00:02:33,020 So let's take a look now 57 00:02:33,020 --> 00:02:34,540 and actually do this request. 58 00:02:34,540 --> 00:02:36,410 And for now, it's not gonna do anything, 59 00:02:36,410 --> 00:02:38,640 so it will not give us tour number five 60 00:02:38,640 --> 00:02:40,490 automatically, of course. 61 00:02:40,490 --> 00:02:41,820 But what I want to do for now 62 00:02:41,820 --> 00:02:44,170 is to just take a look at req dot params. 63 00:02:44,170 --> 00:02:47,690 And so indeed, here we have, ID is five. 64 00:02:47,690 --> 00:02:50,480 So, request dot params is a very nice object 65 00:02:50,480 --> 00:02:53,160 which automatically assigns the value 66 00:02:53,160 --> 00:02:57,600 to our variable, so our parameter that we defined. 67 00:02:57,600 --> 00:02:59,720 And we could actually define multiple. 68 00:02:59,720 --> 00:03:01,703 So we could say ID, 69 00:03:01,703 --> 00:03:04,883 and then we could say X, and we could say Y, 70 00:03:06,360 --> 00:03:10,110 and then we, of course, would have to define all of them. 71 00:03:10,110 --> 00:03:11,750 So 23 72 00:03:11,750 --> 00:03:14,200 and 45, let's say. 73 00:03:14,200 --> 00:03:15,810 Send this guy. 74 00:03:15,810 --> 00:03:17,380 And then, indeed, we have an object 75 00:03:17,380 --> 00:03:20,290 with all these three variables in them. 76 00:03:20,290 --> 00:03:23,680 Okay, now if we define them like this in the URL, 77 00:03:23,680 --> 00:03:26,003 we actually have to then specify. 78 00:03:26,890 --> 00:03:29,060 So if we didn't have the Y parameter, 79 00:03:29,060 --> 00:03:31,180 we would run into an error, 80 00:03:31,180 --> 00:03:35,350 because we are now not hitting this exact route, okay? 81 00:03:35,350 --> 00:03:38,900 So our route is now no longer exactly this, 82 00:03:38,900 --> 00:03:40,730 but it's only this. 83 00:03:40,730 --> 00:03:43,500 And so, there is an error, okay? 84 00:03:43,500 --> 00:03:44,650 Make sense? 85 00:03:44,650 --> 00:03:46,950 Now there is actually one thing that we can do, 86 00:03:46,950 --> 00:03:48,980 and that is optional parameters. 87 00:03:48,980 --> 00:03:51,790 So if you want to make this parameter optional, 88 00:03:51,790 --> 00:03:53,880 we just add a question mark to it, 89 00:03:53,880 --> 00:03:57,283 and now, it is optional, so we no longer have to specify it. 90 00:03:58,410 --> 00:03:59,860 So just like this. 91 00:03:59,860 --> 00:04:02,970 And so now we only have five, 23, 92 00:04:02,970 --> 00:04:05,660 and then Y is still here, but it's undefined, 93 00:04:05,660 --> 00:04:06,960 because it's now optional. 94 00:04:07,940 --> 00:04:08,773 All right. 95 00:04:08,773 --> 00:04:11,610 But let's get rid of all of this, 96 00:04:11,610 --> 00:04:15,430 because we only really need the ID, okay? 97 00:04:15,430 --> 00:04:16,829 And so now all we need to do 98 00:04:16,829 --> 00:04:19,920 is to actually get the tour with this ID 99 00:04:19,920 --> 00:04:23,720 from our data.json, from our JSON file, right? 100 00:04:23,720 --> 00:04:25,580 So basically, from the tours array. 101 00:04:25,580 --> 00:04:28,100 And so that shouldn't be all too hard. 102 00:04:28,100 --> 00:04:29,493 And so let's say, 103 00:04:33,110 --> 00:04:35,400 const tour is equals 104 00:04:35,400 --> 00:04:37,140 to tours 105 00:04:38,320 --> 00:04:39,500 dot find. 106 00:04:39,500 --> 00:04:41,610 And I'm not sure if you are familiar 107 00:04:41,610 --> 00:04:43,350 with this find method here. 108 00:04:43,350 --> 00:04:46,400 So once, more this has nothing to do Node or Express, 109 00:04:46,400 --> 00:04:48,750 it's just a regular JavaScript function 110 00:04:48,750 --> 00:04:50,610 which you can use on arrays. 111 00:04:50,610 --> 00:04:52,270 And the way it works is this. 112 00:04:52,270 --> 00:04:55,160 So in here, we pass a callback function, 113 00:04:55,160 --> 00:04:57,993 and so let me actually start by writing that one out. 114 00:04:59,590 --> 00:05:01,310 So element ID 115 00:05:01,310 --> 00:05:03,260 equals the req 116 00:05:04,410 --> 00:05:06,130 dot params, okay? 117 00:05:06,130 --> 00:05:08,660 So it'll basically loop through the array, 118 00:05:08,660 --> 00:05:10,320 and in each of the iterations, 119 00:05:10,320 --> 00:05:12,950 we will have access to the current element, 120 00:05:12,950 --> 00:05:15,060 and we will return either true or false 121 00:05:15,060 --> 00:05:17,700 in each of the iterations, okay? 122 00:05:17,700 --> 00:05:20,050 Now what the find method will then do 123 00:05:20,050 --> 00:05:21,890 is that basically, it will create an array 124 00:05:21,890 --> 00:05:23,520 which only contains the element 125 00:05:23,520 --> 00:05:27,620 where this comparison here turns out to be true, all right? 126 00:05:27,620 --> 00:05:29,840 And in this situation, we want to find the element 127 00:05:29,840 --> 00:05:32,020 where the ID is equal to the one 128 00:05:32,020 --> 00:05:33,860 that we get from the parameters. 129 00:05:33,860 --> 00:05:36,830 And so by specifying this callback function here 130 00:05:36,830 --> 00:05:39,520 with this comparison, we will ensure 131 00:05:39,520 --> 00:05:42,630 that only the element where the ID is actually equal 132 00:05:42,630 --> 00:05:45,160 to the specified ID in the parameters 133 00:05:45,160 --> 00:05:47,600 will get returned from the find method 134 00:05:47,600 --> 00:05:50,520 and stored into tour, all right? 135 00:05:50,520 --> 00:05:54,070 Now actually, this is not going to work like this, 136 00:05:54,070 --> 00:05:55,620 because as you see down here, 137 00:05:55,620 --> 00:05:58,460 these values here, they are actually strings, okay? 138 00:05:58,460 --> 00:06:01,090 So we need to convert that to a number, 139 00:06:01,090 --> 00:06:02,290 but that's very easy. 140 00:06:02,290 --> 00:06:04,310 All we have to do is to say, 141 00:06:04,310 --> 00:06:08,380 the ID is equal to req dot params 142 00:06:09,330 --> 00:06:12,200 dot ID times one. 143 00:06:12,200 --> 00:06:14,210 So this is a nice trick where JavaScript, 144 00:06:14,210 --> 00:06:17,690 when we multiply a string that looks like a number, 145 00:06:17,690 --> 00:06:19,870 when we multiply that with another number, 146 00:06:19,870 --> 00:06:22,983 it will then automatically convert that string to a number. 147 00:06:23,860 --> 00:06:27,903 So that's a very small, nice trick that we can use here. 148 00:06:29,910 --> 00:06:34,910 And so, now, we already have our tour ready to send. 149 00:06:36,410 --> 00:06:39,320 And so the data that we want to send 150 00:06:39,320 --> 00:06:41,410 is tours equal 151 00:06:41,410 --> 00:06:43,463 to tour. 152 00:06:44,450 --> 00:06:45,770 Or actually, it's just tour, 153 00:06:45,770 --> 00:06:48,440 so tour should be equal to tour, all right? 154 00:06:48,440 --> 00:06:49,570 Make sense? 155 00:06:49,570 --> 00:06:53,050 So maybe the most complicated part here is just this one, 156 00:06:53,050 --> 00:06:56,900 but, yeah, that is again just a normal JavaScript. 157 00:06:56,900 --> 00:06:59,520 And so I believe, or at least I hope, 158 00:06:59,520 --> 00:07:01,710 that you're already familiar with using 159 00:07:01,710 --> 00:07:04,943 this kind of array methods, all right? 160 00:07:06,250 --> 00:07:09,720 So, we only want tour number five. 161 00:07:09,720 --> 00:07:12,590 And so let's take a look if it actually works. 162 00:07:12,590 --> 00:07:15,860 And well, it didn't, so let's take a look at that. 163 00:07:15,860 --> 00:07:20,250 Oh yeah, indeed, here we need to use the ID, of course. 164 00:07:20,250 --> 00:07:22,130 So not req.params. 165 00:07:22,130 --> 00:07:23,440 ID. 166 00:07:23,440 --> 00:07:26,570 Okay, so, try that again. 167 00:07:26,570 --> 00:07:30,970 And now, indeed, we have the tour with ID of five, 168 00:07:30,970 --> 00:07:33,193 okay, and only that single tour. 169 00:07:35,230 --> 00:07:37,390 Let's try number two, for example, 170 00:07:37,390 --> 00:07:39,800 and indeed, ID two. 171 00:07:39,800 --> 00:07:41,800 But now, let's try another situation. 172 00:07:41,800 --> 00:07:44,723 Let's do, ID 23, 173 00:07:45,730 --> 00:07:48,490 and so now, we get nothing back, right? 174 00:07:48,490 --> 00:07:53,050 And that's because there is no tour with the ID of 23, 175 00:07:53,050 --> 00:07:56,300 but we still return the 200 OK status code, 176 00:07:56,300 --> 00:07:59,330 and so, that doesn't make much sense, okay? 177 00:07:59,330 --> 00:08:01,903 And so let's actually go ahead and change that. 178 00:08:02,830 --> 00:08:04,580 And what should we do here? 179 00:08:04,580 --> 00:08:07,180 Well, as a very simplistic solution, 180 00:08:07,180 --> 00:08:09,990 what we can do is to check if the ID 181 00:08:09,990 --> 00:08:13,420 is larger than the length of the tours array, 182 00:08:13,420 --> 00:08:15,030 and if it is longer, well, 183 00:08:15,030 --> 00:08:17,670 then we can send back a 404 error 184 00:08:17,670 --> 00:08:21,753 saying that we could not find any tour for the given ID. 185 00:08:23,030 --> 00:08:26,250 So let's do that very simple solution, 186 00:08:26,250 --> 00:08:28,755 because, well, this is not the real API 187 00:08:28,755 --> 00:08:30,140 that we're building at this point, 188 00:08:30,140 --> 00:08:31,670 and in this case, it's not a problem 189 00:08:31,670 --> 00:08:34,690 to use an overly simplistic solution like that, 190 00:08:34,690 --> 00:08:37,400 because this is not the real thing that we're building here. 191 00:08:37,400 --> 00:08:39,490 We are really just trying to get familiar 192 00:08:39,490 --> 00:08:41,690 with the was that Express works. 193 00:08:41,690 --> 00:08:44,690 And the same thing can actually be said here, for example, 194 00:08:44,690 --> 00:08:46,520 when we... 195 00:08:46,520 --> 00:08:49,140 So down here, where we post a new tour, 196 00:08:49,140 --> 00:08:52,260 in the real world, we would always, always have to check 197 00:08:52,260 --> 00:08:54,210 if the user input is valid, 198 00:08:54,210 --> 00:08:56,680 so if it doesn't contain any malicious code, 199 00:08:56,680 --> 00:08:59,350 or really, anything that we don't want in our wrap. 200 00:08:59,350 --> 00:09:01,890 And again, we're gonna do that, of course, later, 201 00:09:01,890 --> 00:09:03,680 but in this small example here, 202 00:09:03,680 --> 00:09:07,070 I really just want to focus on the fundamentals, okay? 203 00:09:07,070 --> 00:09:10,100 So let's quickly implement what we were just saying, 204 00:09:10,100 --> 00:09:15,100 which is that if the ID is greater than tours dot length, 205 00:09:16,570 --> 00:09:20,730 so for example, let's say we have 12 tours in our array, 206 00:09:20,730 --> 00:09:24,710 well, if the ID is then 13, or 15, or whatever, 207 00:09:24,710 --> 00:09:27,750 well, in that case, the ID is not valid. 208 00:09:27,750 --> 00:09:31,000 So, we say return, and that's because 209 00:09:31,000 --> 00:09:34,180 we want to exit the function right at this point, okay? 210 00:09:34,180 --> 00:09:35,823 And so that's why we say return. 211 00:09:37,370 --> 00:09:40,040 Then we send a status of 404, 212 00:09:40,040 --> 00:09:42,893 because we couldn't find any tour for that ID, 213 00:09:44,106 --> 00:09:46,370 and then a simple JSON response 214 00:09:49,400 --> 00:09:51,563 with a status of fail. 215 00:09:52,740 --> 00:09:56,173 So fail is what we send whenever we have a 400 code. 216 00:09:58,180 --> 00:10:00,053 And then just a message property, 217 00:10:03,580 --> 00:10:04,663 invalid ID. 218 00:10:06,640 --> 00:10:07,820 All right. 219 00:10:07,820 --> 00:10:09,503 And so let's test that now. 220 00:10:10,740 --> 00:10:12,103 So we have our 23. 221 00:10:13,340 --> 00:10:16,900 And, indeed, 404 not found, invalid ID. 222 00:10:16,900 --> 00:10:19,810 So that's exactly what we just did, and, 223 00:10:19,810 --> 00:10:22,093 it looks like it's working, okay? 224 00:10:24,230 --> 00:10:25,860 Another solution that we could 225 00:10:25,860 --> 00:10:27,490 of course implement here as well 226 00:10:27,490 --> 00:10:29,480 would be to just do this test 227 00:10:29,480 --> 00:10:32,163 after we do the tour here. 228 00:10:34,440 --> 00:10:36,100 So we would get the ID, 229 00:10:36,100 --> 00:10:38,230 then we would try to find a tour, 230 00:10:38,230 --> 00:10:39,810 and if there was no tour, 231 00:10:39,810 --> 00:10:42,670 well then we would say that the ID is invalid. 232 00:10:42,670 --> 00:10:45,600 So let's comment out this one, duplicate, 233 00:10:45,600 --> 00:10:48,950 just so that we actually keep both solutions here. 234 00:10:48,950 --> 00:10:52,000 And so we can say, if there is no tour, okay? 235 00:10:52,000 --> 00:10:53,970 So basically, the result here, 236 00:10:53,970 --> 00:10:56,190 if there is no tour found, 237 00:10:56,190 --> 00:10:59,130 well then this variable is basically undefined, 238 00:10:59,130 --> 00:11:01,840 and we can then test for that here, like this. 239 00:11:01,840 --> 00:11:05,790 So if there is no tour, so if tour is basically undefined, 240 00:11:05,790 --> 00:11:09,183 well, then we send back this error message. 241 00:11:10,330 --> 00:11:12,010 So let's test that as well. 242 00:11:12,010 --> 00:11:14,820 And so indeed, it gives us the same result. 243 00:11:14,820 --> 00:11:16,590 Okay, and just like that, 244 00:11:16,590 --> 00:11:18,750 you learned how to specify parameters 245 00:11:18,750 --> 00:11:21,680 in a URL, like this, okay? 246 00:11:21,680 --> 00:11:24,470 Then you learned how to read these parameters 247 00:11:24,470 --> 00:11:27,650 from the URL, by using req dot params. 248 00:11:27,650 --> 00:11:31,650 Well, and then we used that ID, so that parameter, 249 00:11:31,650 --> 00:11:35,140 in order to find a tour with that exact ID. 250 00:11:35,140 --> 00:11:38,210 So that's what we did here, in this line, okay? 251 00:11:38,210 --> 00:11:40,520 So that was the goal for this video, 252 00:11:40,520 --> 00:11:42,843 and so let's now move on to the next one. 19144

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.