Transcript from the "Importing the Application Router" Lesson
>> So we have our stuff here. The last thing we need to do is like, yeah, cool. We made a router, but we have to attach it back to the main app otherwise, it's just floating around, it's not doing anything. So, maybe I said the app is the actual API.
[00:00:17] This is just like a sub API. Well, you have to mount it back to the app. So in order to do that, let's export the router. So export default router like that and then you can go back to server and you can import your router. Like so and you can get rid of this if you want, you can go after it or whatever.
[00:00:44] The thing about creating routes is that order does matter. If you make a route that's the same like, I'll make another one of these, right, okay, copy just said no. I'll make another one of these with the same method, path combination. Which one of these is going to fire?
[00:01:07] They're both targeting the same method path combination. Like what would you expect? If you were the creators of Express, what would you have built into Express to handle this situation where they're both targeting the same thing.
>> I would say whatever is registered first.
>> Yeah, exactly whatever it was first.
[00:01:27] So like, that's actually what's happening. So this one would actually execute first and this one would never get executed. So the order in which you register a listener matters, so top to bottom does matter when it comes to defining routes. And you can think of it as like this every time you do this you're actually registering like, hey, I want to be notified via this function when this combination comes in and they put that subscription inside of an array.
[00:01:56] So there's an array of event handlers that get called. So it would make sense that this one will be the first thing in the array, and this one would be the second thing in the array. So because this one got found first, this one never gets found. You can leave it that way, that's pretty much what it is.
[00:02:12] Okay, anyway, I wanna leave that and now what we wanna do is, we wanna say app.use. Let me talk about that, use is not ASD method, but some of that build it to express and use allows you to apply some type of global options, some type of global configuration, either to the whole app or to a certain path of your API.
[00:02:38] In our case, we're gonna do a certain path. We're gonna say we wanna use or so for everything that has slash API in it, we wanna use the router. So what is this actually saying? Okay, we're gonna walk through it. We know in our router we have let's say we know we have a get request for get/product, right?
[00:03:03] So in our head we're like, cool, if we wanna make a get request to slash product, assuming there was something here, it would trigger this it would work. No not anymore because router is mounted to /API. So the actual URL for that would be /api/product. Because this is mounted before any other routes that are registered here.
[00:03:25] So now this whole router doesn't exist until you put /api in your URL. So you have to put go to your url.com/api/any one of these or these or these. But these don't know that they belong to slash API. And that's what makes router cool. It's just a way for you to abstract and move around different pieces of your API.
[00:03:52] Like components you can think of like a router as like a component for routes basically they're just reusable.