Lesson Description

The "Subrouters" Lesson is part of the full, API Design in Node.js, v5 course featured in this preview video. Here's what you'd learn in this lesson:

Scott demonstrates how to create sub-routers in Express for resources like users, habits, and authentication, setting up CRUD routes and handling habit completions. He also covers HTTP status codes, emphasizing proper use of success and error responses.

Preview
Close

Transcript from the "Subrouters" Lesson

[00:00:00]
>> Scott Moss: OK, well, let's actually make the Routing for the app then. So let me scroll to it, The first thing we're going to do. Is we're going to create Some sub routers, these are routers that we can mount on a top level router, so. We're gonna go into our source file, our source folder, we're gonna make a new route, or new folder, I'm sorry, we're gonna call it routes, and then for each one of the resources that we're gonna have, we're gonna make a file for it, which is gonna be our router.

[00:00:16]
We're gonna go into our source file, our source folder, we're gonna make a new route, or new folder, I'm sorry, we're gonna call it routes, and then for each one of the resources that we're gonna have, we're gonna make a file for it, which is gonna be our router. So we're gonna say user router. Dots here inside the routes or I'll say User routes, let's just call it that, be consistent. User routes, we're gonna have habit routes.

[00:00:33]
User routes, we're gonna have habit routes. We're going to have what else do I have here, have a user. A, yeah, and then we'll. Have all the routes.

[00:00:52]
Have all the routes. Cool. And the way this is gonna look for each one of these is they're gonna be their own router, individual sub router that we can mount onto the top-level router. So the way that we do that is we can import.

[00:01:10]
So the way that we do that is we can import. A named import from Express called router from Express, and then what we can do is we can make a router from router. I don't know, I don't know why they still haven't changed this. It's 2005.

[00:01:26]
It's 2005. Express V5 and they still have you doing. Stuff like this, but I digress, so let's set up our routes. We're basically gonna set up like.

[00:01:40]
We're basically gonna set up like. The CRUD, the CRUD routes for this, right, so for in this case a For the alt routes, this was a little different because it's not like entirely CRUD because Al is mostly just like signing up and signing in. It's not alt itself is not a resource, where it's like users would be a resource, but a itself is not really a resource, it's a mechanism I guess. So in this case we would just say router.post.

[00:02:02]
So in this case we would just say router.post. So we treat the router just like we would treat the app. It has the same methods, so dotpost.git, do patch, do put. It's the same thing.

[00:02:16]
It's the same thing. So we're gonna say post, we wanna register. This is basically our Sign Up route. So we wanna do that, Reres.

[00:02:34]
So we wanna do that, Reres. And then for now we're just gonna mock everything out, right? We're not making this stuff right now. I was gonna say status 201.

[00:02:56]
I was gonna say status 201. Remember anything in the 200 means good, 201 technically means successful post. That means your post request was good. 200 would mean your GET request was good.

[00:03:11]
200 would mean your GET request was good. In practice, it doesn't really matter. So, and you can put whatever you want. Again, this is just placeholder, so I'm just gonna put like, you know, user signed up.

[00:03:36]
Again, this is just placeholder, so I'm just gonna put like, you know, user signed up. Cool. And then we need another one for signing in, so write it our post and we'll just call this. You can call it login, you can call to Sign In, call whatever you want.

[00:03:47]
You can call it login, you can call to Sign In, call whatever you want. I'll just call it log in. And same thing. Copy that bring that down here.

[00:04:05]
Copy that bring that down here. There we go, and then I'll say user. Logged in. And they just need to export this router.

[00:04:27]
And they just need to export this router. So like most Module systems if I didn't talk about this already, if you wanna import a Module, you have to export it first, right, otherwise you can't get access to it A Module is just a closure A closure is just a function. Functions can't reach inside of functions unless the function returns something in the first place. That's what this is.

[00:04:45]
That's what this is. When you export a Module, you're returning something out of you gonna get this whole file as a function that nobody can reach into. And when you export something, you're returning something out of this function that anybody can get access to if they choose. OK, and for the habit thing, we'll do something similar, but we will make CRUD stuff from this one, so this will be from Express.

[00:05:01]
OK, and for the habit thing, we'll do something similar, but we will make CRUD stuff from this one, so this will be from Express. Cost router equals router. And then we'll say router.git, so let's say I want to get all my habits, so I'll just say slash. Notice I didn't put slash habit here.

[00:05:24]
Notice I didn't put slash habit here. We'll talk about that in a second. We'll talk about mounting, so I'll do slash. And yeah.

[00:05:47]
And yeah. Let's do a placeholder. Right. I like JSON.

[00:06:04]
I like JSON. And we'll say, you know, message, you know, here's your habits. Habits. Cool.

[00:06:18]
Cool. So that'll give you all your habits, then we need to get one habit. So I'll just say slash ID. I will say, got one habit.

[00:06:35]
I will say, got one habit. I'll say router. Gets let's do a post, I'm sorry, so now we want to create a habit that's just gonna be to slash. With the post instead of a git, so router.post just to slash.

[00:06:51]
With the post instead of a git, so router.post just to slash. Same thing. Right crust. Great.

[00:07:06]
Great. Have it, if we wanted to do, a delete, you wanna delete a habit, we can say delete. Come here, but in this case, this will need an ID to know which one to delete, so we can put that there and we could say delete it, have its. So on the off routes you did a status 201 on these, is it there must be a default then?

[00:07:21]
So on the off routes you did a status 201 on these, is it there must be a default then? Oh yeah, they're all, they're all for each one of these they're gonna default to 200 cause it'll you sent anything non error back, it's always gonna default to 200, but yeah, I mean to be, you can put a 201 on the post if we want to be. You know, consistent, but yeah. There's also like a specific status code for a successful delete too.

[00:07:37]
There's also like a specific status code for a successful delete too. It's 2 something. I don't know my guess tells me it's like 209 maybe, I don't know, but anything 2 is success. You typically won't see anything other than 201 and 200.

[00:07:40]
You typically won't see anything other than 201 and 200. I almost never seen anything else unless someone who's like. Really locked in on like getting their servers down right, they're just like. I'm gonna do everything right, like you almost will never see it, in my opinion.

[00:07:40]
I'm gonna do everything right, like you almost will never see it, in my opinion. Usually the one that people really focus on is the 400s. They get those very specific because those are user facing. That means your request is jacked up, so they really want to nail those down, but like everything else is like they don't really care.

[00:07:40]
That means your request is jacked up, so they really want to nail those down, but like everything else is like they don't really care. You got another one in here for this is where I kind of break free of. REST for a little bit, so I want to be able to complete. A habit, so I can say for this habit with this ID.

[00:07:40]
A habit, so I can say for this habit with this ID. I want to run this command, the complete, so I'll say. Slash compete and I can do that, you know, whatever I want here. That's the whole point of working on servers like.

[00:07:40]
That's the whole point of working on servers like. There really are no rules, like, I just make this up.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now