
API Design in Node.js, v3 Router & Sub Routes Solution
This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Router & Sub Routes Solution" Lesson
[00:00:00]
>> Scott Moss: During the break, we were tasked with completing the crud routes for the item resource that we have and right now you'll see that there's just a blank router here. So what I'm gonna do is I'm gonna walk through my solution of how I would have completed this to get the test to pass.
[00:00:17] So feel free to follow along, but the same solution is also going to be on the lesson-2-solution branch where you can check out too and see there. So I'm just gonna go ahead and get started. So because we wanna mount this router at the /api/resource name, in this case item, we're gonna mount it at that path.
[00:00:37] There's actually a lot of different ways that we can do it. And how you do it is gonna determine what you can do later with your routers and how you organize your code. So I'll walk through some different scenarios of the different ways you can do it and then I'll also walk through how I'm gonna do it and why.
[00:00:52] So the first obvious scenario is that you might start naming the full route path here with this router because that's very obvious, cuz it's a router and we're gonna put the full route path. So you might do something like this. In this case I'm just gonna do get for now.
[00:01:09] So you might do something like /api/item. I'm gonna do a get request to that. And there's nothing wrong with that. You can totally do that and you can do that for every single route verb combination we might have slash api slash item slash id for a post. I'm sorry, for a put, slash api slash item for a post, you can do that all the way down.
[00:01:31] The issue is what's gonna happen is when you go mount that on your server, where would you mount that? So the only way you can mount that is you would have to do, you would have to realize that you could do itemrouter like this without a path. So you would import itemRouter and then you would pass in without a path at all because the item router itself determines the full path.
[00:02:00] So that's how you would do that. So you could do that, and that way this file doesn't have to know about any of the routes. It doesn't have to know about any of the names. You can just abstract it completely away. So that's one way. But the way that I'm gonna do it is I'm going to do have this router not know about where it's mounted, it's just gonna know about it's relative route.
[00:02:22] It's not gonna know about its parent routes. So basically what that means is, instead of it knowing about /api/item, and having control over that, all it knows about is its relative route, in this case it'll just be slash. So by the time this router executes, it's already gonna be at /api/item.
[00:02:39] And then this router runs, so it's just relative. So in this case, I'm gonna do that and I'm gonna use the .route method which I showed you all before we headed off. And I'm gonna say, cool, for a .route to this path which is just the index. So in this case, because we're gonna mount it at /api, /item, this route right here, just a slash, is equivalent to /api/item.
[00:03:05] It's equivalent to that cuz that's where I'm gonna mount this router. I'm gonna mount this router here, /api/item. So that means this route is equivalent to this. It's the index for this. So I'm gonna route there. And I know I need a get request to be able to get many.
[00:03:23] And then I need a post request to be able to create one, so that's one. And then I needed a router.route('/:id') which is gonna be equivalent to / API/item. It's gonna be equivalent to that or /ID, sorry. So that's gonna be equiv because again, this is gonna be mounted at /API/item.
[00:03:50] So this is what comes after. Express is gonna combine the routes for me. So now that I have that, I can go ahead and do my put request, which is the update. I'll do my delete request which is to delete one. Now I'll do my get request which is to get one by an ID.
[00:04:08] So now that I have all those, I'm gonna go ahead and just mock out these controllers right quick. I'm just gonna make one mock controller that just does nothing.
>> Scott Moss: And I'll just res.send a message.
>> Scott Moss: There we go. And I'll just add the controller to all of them.
[00:04:33] If you don't have a controller here that sends something back and you try to run these routes, you might not get a response, actually you won't get a response, it will just hang. So if your server was hanging even though you know your routes were right, you were not getting a 404, it's cuz you didn't send something back in a controller.
[00:04:48] So I was gonna send something back, put a fake controller on all of these, have it export. So now that I'm exporting, and then the sweet thing about the E6 stuff here is that because I'm exporting as a default, I can import it whatever name that I want, because it's a default export.
[00:05:06] If it's a named export, as in, I got rid of line 20, and I put the word export in front of router, then now I have to import it with the name router. But on the import, I can also change the name there using the as keyword, import as something else.
[00:05:22] So there's a lot of ways you can do that in the S6, I'm just gonna import it, or export it as a default here. Because there's many routers in these different resources and have them just be the same as a lot better to have them think of what the name is to import it.
[00:05:37]
>> Scott Moss: So now that we have that, the next thing we need to do is actually import that router. So because it's a default, I can call it whatever I want. I'll call it the item router from,
>> Scott Moss: Resources/item/item.router. So we've got the item router there. And then I'm going to mount it at /api/item and now we have the item router.
[00:06:10]
>> Scott Moss: So if we run our test,
>> Scott Moss: our npm run
>> Scott Moss: Test-routes, I believe.
>> Scott Moss: You can see, it passed. There was only one test for that, and it passed. If we just go look at the test one more time, you can see it's testing that there is these combination of routes.
[00:06:40] So it's expecting a path of this with a method of git. This path with a method of git and so forth and so forth. So we satisfied that here. So here's all the routes with the different verb methods, and that's why the test passed. And now let's actually run it, we'll execute this.
[00:07:01]
>> Scott Moss: And I'm gonna head over to 3000 and do a slash api slash item and I'm gonna run that. And you can see I get a message hello. So that works good. So that's a get request. Then I had another one for slash ID. So I'll put slash anything here.
[00:07:19] That's also a get request, so that's good. Then there was a post request to just /item, so I'll run that, that's also good. And then we had a put request to /item, with some ID, so we run that as well. And then, last one was a delete request to /item with some ID.
[00:07:40] And if we run that, that one will. Anybody do it differently? Cuz there's a lot of ways, there's not one right way to do this. There's a lot of ways which is why I love Express.