‫Let's now refactor our code a little bit.
‫So basically, reorganize some of our route
‫to make the code a lot better.
‫So right now, we have all these routes here.
‫So the http method plus the url
‫together with the route handler,
‫which is this callback function.
‫Right?
‫And we have these routes and route handlers
‫kind of all over the place.
‫So we have this and then this and all after the other,
‫but it's kind of difficult to see
‫what route we actually have in our code.
‫So all the routes should kind of be together,
‫and then the handler functions also together.
‫So separate from these routes here.
‫Okay.
‫So what I'm gonna do is to go ahead
‫and basically export all of these handler functions
‫into their own function.
‫Okay?
‫First of all, let's also get rid of this
‫old code that we had here.
‫And so, yeah.
‫Take this.
‫Or actually, not just this.
‫So all of this.
‫Take this, and create a new function out of it.
‫So I'm calling this one getAllTours.
‫And then I'm assigning it this function.
‫Give it a save, and now here
‫instead of passing the callback function directly,
‫well, I just do getAllTours.
‫And that's it.
‫Okay?
‫And so right now, if I'm gonna hit this route here.
‫So this endpoint.
‫It will then simply call this getAllTours function.
‫So the functionality didn't change at all,
‫but I did slightly refactor my code.
‫Okay.
‫And you will see how nice this is gonna be
‫when all of the routes are together
‫and all these route handlers are also together.
‫So...
‫This one is getTour because this is only to get one tour.
‫Right?
‫Add to here, getTour.
‫So.
‫And we're starting to see different routes
‫that we have in our project at one glance.
‫So.
‫This is the next one, and it's gonna be createTour.
‫Const createTour equals all of this.
‫And here then createTour.
‫Next up we have...
‫Not like this.
‫We have updateTour.
‫All right.
‫And so this also actually makes our code
‫a little more clartive.
‫Right?
‫So we can see very easily what happens here
‫when we're actually reading the code.
‫So when we read this line of code,
‫we immediately see, oh, this will update to tour
‫because we actually have the function name here.
‫Okay.
‫And now finally, we have deleteTour.
‫Okay.
‫DeleteTour.
‫So.
‫This looks a lot nicer already,
‫but it's still not perfect.
‫We can do even better.
‫Because, let's say, that we want to, for example,
‫change the version or the resource name.
‫We would then have to change it
‫in all of these five places,
‫and that is not ideal.
‫And so instead of having all of this,
‫we can do something better.
‫So let me actually start it from scratch,
‫and write app dot route, so that's a new one.
‫And so here we specify the route that we want.
‫So let's start actually with this one here.
‫And then, on there is where we specify
‫what we want to happen on get.
‫Dot get.
‫Well, we want getAllTours.
‫So.
‫What we have here is right now
‫exactly the same as we have here.
‫Okay, so that's coming out this part.
‫So it's exactly the same.
‫Now, the thing is, that here we can now
‫actually chain the post method.
‫And then we can say createTour just like this.
‫Okay?
‫And so just like this,
‫we have now actually created an even better way
‫of writing a route because right now,
‫this is not repeated for the get and the post.
‫Okay.
‫Let's give it a save.
‫It's nicely formatted, and even easier to read than before.
‫Okay.
‫So.
‫The two route handlers that we use
‫on this kind of route here.
‫So without the id, is only for get and for post.
‫So for getting all tours, and for creating a new tour.
‫And now, as you can imagine,
‫let's do the same for the other route.
‫So basically at this url here.
‫So we add this url, and now,
‫we will have get, patch, and delete.
‫So this one is getTour.
‫This one is updateTour, ah it's already there.
‫And finally, I have deleteTour.
‫So deleteTour.
‫Give it a save.
‫And we can now go ahead and...
‫Well, I'm not gonna delete it.
‫So that we can keep the old code here as well.
‫But, this is gonna work just the exact same way,
‫but making the code a lot better,
‫and also easier to read.
‫Right?
‫So again, just to recap, we separated our handler function
‫of the route itself so that we can later actually,
‫even export these into another file.
‫Okay, so that will then make the code even better.
‫But for now, let's keep it at this.
‫So with the handlers nicely separated from the routes.
‫And then, we specified all the actions
‫for each of our two routes.
‫Which are simply tours
‫and then tours slash the id parameter.
‫So.
‫Just to make sure it still works,
‫let's use this one here.
‫And with a tour that actually is this.
‫But you can see already it's not giving us an error
‫and so it actually still works.
‫Same four getAllTours because that's another url.
‫And just to make sure it still works as well.
‫Indeed, it does.