‫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.