All language subtitles for 013 Nested Routes with Express_Downloadly.ir_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Let's now improve the nested

route implementation that we coded in the last lecture.

And for that,

we're gonna use a special advanced express feature.

So, as you remember, in the last video

we implemented a simple nested post route,

so just this one here.

Right. And so this means that the review route

is kind of within the tour route.

And again, because reviews belong to tours in a sense.

Right?

And so this is a very common thing to do in Api design.

Now, the problem with this implementation is that it is,

of course, a bit messy.

And that is because we put a route for creating a review

in the tour router.

Simply because a route starts with slash tour.

So that's a bit confusing,

and what's also confusing

is that we have something very similar to this here,

also in our review route.

So, basically this here.

So when we create a new review without the nested route,

this piece of the code here is actually exactly the same

as this one. Right?

And so, besides this being a bit confusing,

we also have duplicate code which we would have to maintain

in two separate places in case we wanted to change anything.

And so again, that's a very bad practice.

And so let's now fix this using an advanced express feature

called mergeParams.

So first off let's remove this code here

which doesn't really belong to the tour router.

Next up, we will actually import the review router

into this tour router.

Okay?

So right now we are importing the review controller,

but actually we want the review router.

So let's get rid of this one,

and actually I'm going to delete it altogether.

And now,

the review

router.

And okay.

So, let's actually get this code

and put it here at the top,

just so that we see what we're doing,

because we will actually create some new code

right here at the top.

And so what we're gonna do here

is to say

router.use.

And so we will basically say that this tour router

should use the review router

in case it ever encounters a route like this.

And

Review routes.

Did I call it routes or router?

Should be called router.

Okay?

So, keep in mind that a router itself

is really just a middleware.

And so we can use the use method on it,

and then say that for this specific route here,

we want to use the review router instead.

Okay?

And so this is actually again mounting a router.

So, if you take a look at that,

here in the app.js,

that's actually exactly what we did.

So we did .use,

then the URL,

and then the router.

And so here, what we're doing

is actually the exact same thing.

So this is the tour router, and so then we say

well whenever you find a URL like this,

well, then just use the review router.

And so, right now, when we have a URL like this,

it will start by getting into the tour router here,

okay, because, again it starts with slash tours,

okay,

so basically it's rerouted into the tour router,

then when it reaches the tour router,

then it will match this URL here,

and then it will again be rerouted into the review router.

And like this, we have the tour router

and the review router nicely separated

and decoupled from one another.

But now, there's actually still one piece missing

because right now this review router here

doesn't get access to this tour id parameter, okay.

And so now we need to enable the review router

to actually get access to this parameter here as well.

So let's now move to the review router, okay.

And so this is where the medical mergeParams

that I mentioned right in the beginning

comes into play.

So here, in the express.router function,

we can specify some options,

and here all we need to do is set mergeParams

to true.

And that's it.

But why do we actually need this here?

Well, it's because, by default,

each router only have access

to the parameters of their specific routes, right.

But here, in this route, so in this URL for this post,

there's of course actually no tour id.

But, we still want to get access to the tour id

that was in this other router, right.

So this here.

And so, in order to get access to that parameter

in this other router,

we need to physically merge the parameters, okay.

And so that's what mergeParams, set to true, does.

And so now, no matter if we get a route like this,

so let's just copy it here,

Again it's easier to visualize it with an example.

And so again, no matter if we get a route like this,

or a route like this,

it will now all end up in this handler here, okay.

So in the end basically to this handler function.

And again that works because all of the routes

starting with this kind of pattern here

will be redirected to this router

exactly in this line of code here, okay.

So we redirected to the review router here

and so then in there it will match to this exact route,

okay.

And thanks to merge parameters,

we then get access to this id

which actually comes from the other router before.

Great.

So let's just do some clean up here,

actually get rid of this,

this here,

and

let's quickly try it out now.

So I'm gonna create a new review here

and let's try another tool this time.

So the city wanderer.

And all we need to do is to change

that id right here in the URL, right.

So, the rating is gonna be three,

and then

was kinda okay.

All right, and so this review will still be done with Jonas,

so from this account, okay,

because that's still one

that is logged in at this current moment, okay.

So, let's send that,

and indeed here we get the tour that we just specified,

and still the same user.

So that's again just to make sure check the tour

to see if it now gets this review

and yeah, it did.

And so that merge parameter trick, that we just implemented,

actually works just fine,

and as intended.

Now to see this in action, even better,

we're gonna adapt the henry function

for getting all the reviews for a specific tour

right in the next video.

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