‫(Man) So in this lecture, you're gonna learn how to
‫chain multiple middleware functions for the same route.
‫So up until this point, whenever we wanted to define
‫a middleware, we only ever passed one middleware function.
‫So for example here, for handling this post request,
‫well we only passed in this middleware function
‫which is our createTour handler.
‫Okay? And so that's the only function that is gonna
‫be called whenever we get a post request.
‫Right? But lets now say that we want to actually
‫run multiple middleware functions.
‫Now you might ask "Why would we want to do that?".
‫Well we might, for example, run a middleware before
‫createTour here to actually check the data that is
‫coming in the body.
‫Right? So a bit similar to what we had before.
‫So we did this check ID middleware before in order to
‫check if the ID is actually valid and doing so outside
‫of the actual route handlers so that they are
‫only concerned with getting, updating, or deleting
‫a tour.
‫Right? And so here, in this specific example with post, we
‫might want to do the same thing.
‫So as I said, we might want to check if request.buddy
‫actually contains the data that we want for the tour.
‫Okay? And so lets do that.
‫And actually, I want you to first do it on your own.
‫So this is kind of a challenge that I want you to complete
‫in order to use the concepts that we've talked about
‫so many times at this point and create your own
‫middleware function.
‫So let me just write it here as a comment.
‫So I want you to create
‫a checkBody middleware function
‫and in there I want you to check
‫if the body contains the name property for the tour name
‫and the price property.
‫All right? And if not, I want you to send back a
‫four-hundred status code.
‫If not, send back four-hundred, which stands for
‫bad request.
‫So basically, an invalid request from the client, which is
‫in this case, trying to create a new tour without a name
‫and without a price property.
‫Okay! And that's actually it.
‫And (laughs) actually not.
‫So I also want you to add it to the post handler stack.
‫Basically. Okay?
‫So I want you to add it in here.
‫And I will actually tell you how to do that
‫because we didn't do it at this point.
‫So its very easy. Lets say our function is called middleware
‫and so what you have to do is to simply add that function
‫here before the createTour handler that will
‫ultimately create the tour. Okay?
‫So this way when we have a post request for this route,
‫it will then run this middleware first and only then
‫the createTour.
‫Okay? So that's how we chain two different middlewares.
‫But now I want you to go ahead and uh create this middleware
‫actually called checkBody.
‫So pause the video now and please try to do it on your own.
‫Okay! So I hope you were successful doing that and then you
‫tested it and that everything works.
‫So now let me go ahead and implement it in my way.
‫So we have this error here but don't worry about it.
‫Its because of this middleware that's not defined.
‫And so let me do it here right after the checkID middleware.
‫So exports.checkBody.
‫We have to request response and next.
‫And so its actually quite simple.
‫So if there is no request.body.name,
‫and I think that's the name but lets quickly go
‫ahead and check that.
‫So it is here is tour simple.
‫Yeah, and indeed its called 'name'
‫and the other one is 'price'.
‫So if there is no request body name or
‫no request body.price
‫well, in this case, we will, just as before, return from
‫this function and send back a response.
‫So the status here is remember: four-hundred add then
‫some 'json' where the status is fail.
‫So that's always when we have a four-hundred code.
‫Add then the message
‫"Missing name
‫or price".
‫Okay? So price and name need to be there at the same time.
‫So if either the price is not there or the name is not there
‫then we already send this error message.
‫Okay? But if everything is correct, well then we want
‫to move on to the next middleware.
‫And so that's what we say here.
‫And then next middleware will of course be createTour.
‫So this one.
‫So, lets go ahead and then add it here.
‫So tourController.checkBody.
‫All right so now the error is gone and we should be able
‫to test it now. So lets close the ones we're not interested
‫in and create a new tour.
‫So right now we have our body with the name, duration, and
‫difficulty but there is no price.
‫So we should now get our error. Right?
‫So lets test that. Send. And indeed, we have 'Missing name
‫or price' with our four-hundred bad request.
‫Okay? And so lets analyze what happened here again.
‫So our request hit this route here and it was a post
‫request. And so, it then run this middleware function
‫and from this function, we then send out this uh response
‫saying that, uh, well the name or the price is missing.
‫Okay? But if now add the price...
‫...so it doesn't really matter what price,
‫send it, well then everything works just fine
‫and we get a 201 created.
‫So what happened this time? Well, it checked for the body
‫and this condition was not true and so it called the next
‫middleware in the stack which is createTour.
‫So it created a tour and sent back the result
‫and like this, finished the request-response cycle.
‫Right? So I hope that was a, uh, nice small challenge and
‫we will be using this kind of logic here all the time
‫where we chain multiple kind of handlers here for the
‫same route. So for example here we could check if a
‫certain user is logged in or if he has the privileges, so
‫the access rights, to even write a new tour.
‫Or really all kinds of stuff that we want to happen
‫before the tour is actually created.
‫Okay? And once again we do that because we want to take
‫all the logic that is not really concerned with creating the
‫new resource outside of that handler. So that that handler
‫is only concerned really with the work
‫that is supposed to do.