This course has been updated! We now recommend you take the API Design in Node.js, v4 course.

Check out a free preview of the full API Design in Node.js, v3 course:
The "Router & Sub Routes Exercise" Lesson is part of the full, API Design in Node.js, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to create routes and sub routes such that the tests pass in the exercise.

Get Unlimited Access Now

Transcript from the "Router & Sub Routes Exercise" Lesson

>> Scott Moss: So we're gonna head over to the next exercise. So check out to lesson two. And if I didn't say it already, if you haven't looked instructions for every exercise on what you're gonna be doing is in the readme with like check marks on showing you exactly what you got to do and what not.

[00:00:19] This exercise and going forward is gonna be a little different from the first one. The first one, we didn't really have any test associated with it, it just to get you to keep your feet wet. Going forward is gonna be test associated with every single exercise and you got to those test to pass.

[00:00:31] So in this example, if you check out to lesson two
>> Scott Moss: And if you all look at the readme. So if we look at this one, two things to know is it's always gonna say the branch that you need to check out to. So in this case lesson two and it is gonna say the test command that you have to run.

[00:01:00] There is a test command just for running all the test. How come it's not running that? Because it will run all the test for the future branches too which obviously would fail cuz you didn't write code for those yet, and then you'll just be like, why are these tests not passing.

[00:01:12] So if you just run the command that I have specifically for that lesson, it will only run the test for that lesson. And those are only tests that you have to get a pass. So in this case the test command that you have to write. If you are using yarn, it's yarn test dash routes.

[00:01:25] If you are using npm it's npm run test-routes. So that's the command that we've been running to run the tests. So if you check out after lesson one and you run a test command, you'll see how much tests they got skipped. Ignore those we need to see some tests that failed.

[00:01:38] Those are the ones that you have to get to pass. Now to get those to pass, basically what you're gonna be doing is you're gonna be creating some routes and sub-routers for our soon to be database resources using the express routing and routers. So there's a checklist here.

[00:01:51] So you're gonna create a router for the item resource, and I'm gonna walk you through all this. You're gonna create full crud routes and create placeholder controllers which just don't do anything, just send back a message kind of like what we did already. Then you're gonna mount that router on the root server, very much like I just showed you all.

[00:02:08] And then you could go and ensure that all tests pass by writing the test command. But if you do all these things above, these three things, all the tests should pass. So what does that actually mean? Well, if you go int the source slash resources, you will see three weeks resources in here, you'll see item, list and user.

[00:02:25] You get this thing API as basically like we're building API for like to do app. So we have users and we have items and then we have a list of items, those are our three resources. User and list are mostly filled out, they're mostly complete for you, so they're here mostly for examples for you just to look to see how things are organized and how they're created.

[00:02:46] But what we're going to be working on mostly in this whole course is going to be the item resource. So if you look here, you click on item, there's a few things. We have a controller. We have a model and we have a router. So I organize it in that way because that's how I separate the resources out for my REST API.

[00:03:04] So for every resource there's gonna be controllers associated with them. There's gonna be models that the controllers use to query and to crema on the data. There's going to be a router that controls how that resource is accessed, so that's why it separated it out like that. So we're going to be working mostly is going to be in the router file.

[00:03:20] So if you go to that file, it's got these three lines, that's like literally nothing in here. It's just a router that's being exported. So it's going to be up to you to create those crud routes. So remember, you're only really gonna have two routes /resource, /resource/id, but with five different valid combinations.

[00:03:38] So it's up to you to figure out how you want to do that using this router or you can do the .route method that I showed you or you gonna explicit define each one of them yourself. As far as controllers you can just mock them out you can just send back hello.

[00:03:49] Doesn't matter what you send back right now in the controllers. But they have to be there or otherwise the server will hang. So you have to be there. You don't have to put anything in the controllers. And that's the first part. The second part, what we're gonna have to do is we have to tape this router that's been exported, and you're gonna have to register it here on the root server.

[00:04:09] All right, so here's the root server. You have a router that's been exported you have to register here. Just feel free to register at /api/ the name of the resource, right? So the resource in this case is called item then you would mount that up /api/item, and that's where you'll mount that router, right here on the root server.

[00:04:29] So once you do all that you should get all the test to pass and you should be able to access and do all the crud on the item resource
>> Scott Moss: And again, if you go look at the list router and the user router, there's some really good examples of what that might look like if you get lost.

>> Scott Moss: For extra credit if you want to, feel free to go ahead and register the list router and the user router on the route, the root server as well. You don't have to, that's just extra credit, it's mostly the same thing as items resource. But if you want to you can register those as well, they are already created for you, they are already done, if you want to register them go ahead, they are already registered on the session.

>> Scott Moss: Any questions on that? So again, if I run this test, npm run test, what is it, test router? No, test routes, there we go.
>> Scott Moss: You can see npm fails because I have this failing test item router has crud routes. So I clearly don't have any crud route.

[00:05:40] If you wanna go look at the tests inside the item photo, there's an underscore, underscore tests, underscore underscore. Click on itemrouter.spec.js and you can just look at the tests and see what's expecting. So you can see there's some router that's expecting what's in verb and it's testing this presence to see if they got registered, if they haven't registered it's gonna fail.

[00:05:57] So if you register all those routes, you'll get a pass here.