Check out a free preview of the full REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course:
The "Excercise: Express Routing" Lesson is part of the full, REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course featured in this preview video. Here's what you'd learn in this lesson:

In the challenge you'll be creating your own express sub router modules to route different paths to resources in the API. - https://github.com/FrontendMasters/api-design-node-v2/tree/lesson-2

Get Unlimited Access Now

Transcript from the "Excercise: Express Routing" Lesson

[00:00:00]
>> Scott Moss: The challenge we're gonna be doing is we're gonna be setting up routing. And basically, what you need to do for this one is, I'm just gonna walk you through it. I'm just gonna get, stash this, actually, I'll leave this up cuz I think it'll be very helpful.

[00:00:11] Will this be helpful, to see this? Okay, I'll leave this stuff up actually. So what we're gonna do, is if you navigate over to API. You'll see tons of stuff in here, don't worry about it. But you should see a file called restRouter. Right now restRouter is, there's one example here that you can follow, and you're gonna use the example to build out the other end points.

[00:00:37] But basically, the API that we're gonna be making is a music API. There's songs, there's playlists, and there's users.
>> Scott Moss: Right now, you can follow the example that I have for the userRouter. So basically what I'm doing here is I'm creating a sub-router inside of the restRouter. And the reason I called it restRouter is because we're gonna make a GraphQL router tomorrow.

[00:00:59] Any other way I would not call it a restRouter, so if you think the name is ridiculous that's why. [LAUGH] I'm separating the two for tomorrow. So this is our restRouter and I made a new one. And I'm mounting another sub-router on that sub-router underneath the user's path.

[00:01:15] So basically, what you would need to do is, one, first thing first is you need to mount that restRouter on the api path on the app, right? So inside of this server.js, you need to mount the api router on the, I'm sorry, the restRouter, get it right. On the /api path.

[00:01:45]
>> Scott Moss: So mount the restRouter on the /api path, that's the first thing. And then if you go look at that restRouter, like I said, this user example is fully fleshed out. You're gonna do the same thing for songRouter and playlistRouter. We need two different mounts for those, so we should be able to access the playlistRouter underneath the /playlist mount.

[00:02:06] We should be able to access the songRouter underneath the /song mount. And then if you actually go look at those individual routers, for instance let's go look at the user router in resources/user. So resources/user inside the index file, you'll see that it's just pointing to the restRouter. So if you go to the restRouter there, you'll see here, you see some pretty crazy stuff in here.

[00:02:31] Now I'm just gonna walk through this stuff briefly. And I didn't really wanna talk about it because I want I wanted to give you enough for you to figure out what's going on here, and I think I gave you enough. But basically, this is a fully fleshed out sub-router for the user resource.

[00:02:45] You're gonna use this again as example to flesh out the routers for the song. If you go look at the song REST router, it's empty. If you go look at the playlist REST router, it's also empty. But it's gonna look pretty much identical to the userRouter, just using it's own router.

[00:03:01] So let's walk through here. I didn't talk about param. Param is basically a way that we can tell our router or express, and say, hey somebody comes in here with the URL and it has a param on it, like id, run this function. That's what it's saying. This function allows us to pick off the parameter in the URL, do something with it, and send it back down the router.

[00:03:24] So this doesn't respond to the request, it does something with that parameter. Usually like for instance, let's say you wanna get user/1, right? This is gonna take that 1 parameter, go to the user database, grab the user with id of 1. Attach it to the request, call next, and send it down to the next thing that's there.

[00:03:44] So that's what it's doing. This is like a middleware almost, but we haven't really talked about middleware. So you don't really need to know what any of these functions are doing. You can just put them there. Don't worry about it, they'll work. We gonna get to these functions later.

[00:03:57] But basically, you just need to set up the routes and the handlers. The other thing is, is this shortcut here that I did not talk about. I also did not talk about this cuz I didn't want you to use a shortcut, but if you understand it, you can totally use it.

[00:04:09] This shortcut on any router allows you to use the .route method. And it's just a shortcut so you don't have to do this. If we didn't use a .route, we'd have to say userRouter. Let's say userRouter('/') or userRouter.get('/'), and then you'll have to do userRouter.post('/'), it's the same route, right?

[00:04:29] We're gonna eventually do two different controllers, these are gonna be two different actions here, but on the same route. So .post allows you to, I'm sorry, .route allows you to say hey, for this route, I wanna do this get, do this get, do this post. You don't have to define the route twice, that's all it's doing, so it's chainable.

[00:04:49] So you can say, hey, for index and for get, I wanna do that. For index and post, I wanna do that. The same thing for this route right here, which is a parameter. So for a /:id, get, post, delete, right? So again, this is a fully fleshed out userRouter.

[00:05:06] And you need to flesh out the songRouter and the playlistRouter. And then you also need to hook them up in the restRouter.js file, very similar to what's going on here with the userRouter. And then finally, you need to hook up the REST router to the app. Right, so you've got five things to do.

[00:05:30] Build out the songRouter, build out the playlistRouter. Attach the songRouter to the restRouter, attach the playlistRouter to the restRouter and then attach the restRouter to that. And the userRouter is a full example of how to do all of it. So you can look at that, experiment with it.

[00:05:50] There's definitely some stuff in there that I not teach you about. But I want you to play with it, and see what you can think of. Cuz then we'll walk through it and I wanna see how you all think it works, or how you know it works.
>> Speaker 2: Is this lesson two in the Git check out?

[00:06:04]
>> Scott Moss: Yes, this is lesson two, sorry. Yeah, check out on lesson two.
>> Speaker 2: It looks like it cares about Mongo on this lesson.
>> Scott Moss: It cares about Mongo on this lesson? Okay, so make sure you have Mongo running on this one.
>> Scott Moss: Yeah, just make sure you have Mongo running, you should be fine.

[00:06:17] If you don't have Mongo, then what you need to do is you need to go into, first need to go to mLab. So if you don't have Mongo, set up one on mLab, it's free. And let me check out here and show you where you gotta add your code.

[00:06:34]
>> Scott Moss: So yeah, make a Mongo thing, and then go into db.js, and just add in your Mongo URL there. If you have Mongo just start it up, you're fine. If you don't have Mongo, go to mLab, create a free database, get the URL, the connection stream, go to db.js and add it there.

[00:06:54]
>> Scott Moss: If you don't feel like doing all that, that's totally fine. You can also just go to server.js and see online, where it says connect, where it's invoking that function it says connect, you can just comment that out. That connects to the database, you don't need it in this example.

[00:07:10] So you can also just comment that out. We're not at databases right now.