This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Routers" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Speaker 1: So routers are awesome, this entire time we've been using the actual express application for our router, which works great, as you can see. But with the introduction of Express 4, they introduce sub routers or just application routers in general. You could think of a router as a piece of routing functionality and its own middleware stack.
[00:00:29] So right now we have this application level, middleware stack. So everything in the application runs through this. But with a router, we can isolate an entirely different set of rules as far as middleware and routes, and isolate it, completely separate. Think of it as like a component of routers.
[00:00:48] It's like its own little server inside of a server. It still uses its parents' middleware, but it also has its own middleware that nobody else uses. You'll see as we start writing it, how that makes sense, and why it's amazing. So this is why I was saying that Express 4 is actually a really good contender as far as writing an API, whereas Express 3 was kinda limited, because it didn't have that, you had to put everything on app, and that was just like, eh, okay, okay.
[00:01:13] Express 4 is a lot better at it, cuz you can do these mini-routers. So yeah, we can now have more than one router in our application. Think of a router as a module with its own routing and middleware stack and functionality. It's like having a lot of those apps that have their own stack in many places.
[00:01:33] This is great and allows more fine grain control over our resources. Also great for our versioning our APIs. So routers can set up routing, that's why they call it routers, and middleware the exact same way as we talked about with the app. So everything that we've done here works exactly the same way with routers.
[00:01:53] It's literally the exact same thing. They're just not on an application level. They're just on a local level. Super typo. A router is a little different than the entire app, as previous examples because the entire app comes from the implication of Express, and we still control global middleware and configuration and also, it will also set up routing for other routers we make.
[00:02:21] So what I'm saying, there is the app right here that we got from Express is still gonna set up all this global middleware and it's also gonna set up the routes for our sub-routers, as you'll see in a minute. So we still need this app. It's still gonna come in handy.
[00:02:41] Otherwise we'll be writing a lot of boiler plate code. So, a good pattern is to define routes on the global app or a router and pass control to another router that has its own config. So what does that actually mean? So this is the pattern something like we've been doing before.
[00:03:01] We have Express, we make an app. We've done that. The new thing is this, so now what we do is we make a new router. We say express.Router, which turns the router instance. And then what we can do is we can do the same thing we do in the app, I'm gonna say when somebody does a get request to root on this router, just res.json the todos.
[00:03:25] But right below it on app, I'm saying app.use(/todos, todosRouter). So, this is called mounting, so what I'm doing is I'm mounting this router to this URL. So when anybody does any request with any verb get, put, delete, post to this URL, use this router. So I'm gonna walk through what happens.
[00:03:47] So let's say I do a get request to slash todos. Okay so I do a get request to slash todos, it comes through here, it comes to app, it's like, here's the first middleware, slash todos you want me to use this router great, let me go up here.
[00:03:59] So it comes to this router and it's like, yeah, I did a get request. And then this route right here, this slash, it's not actually referencing the route slash, it's referencing the root of this router, which is todos. So when I say todoRouter.get, root, I'm actually saying todo.
[00:04:16] I'm actually saying app.get/todos. So the root of todosRouter is whatever was mounted on it. Right? So, if I were to put slash ID right here, the full URL would be /todos/ID.
>> Speaker 1: So this will not trigger if I did a get request to just root. It will trigger if I did a get request to /todos, which is the root of this router.
[00:04:45]
>> Speaker 2: So if you're expecting a get of an ID you put colon and ID?
>> Speaker 1: Yep, exactly, you put colon ID right here and that would be slash todo, slash ID. So I want you to start, when you see this as a URL, start thinking of it as root, that's what that means, root.
[00:04:59] It doesn't really mean slash, it means what's the root of this? So the root of the todo router is whatever it was mounted on which was slash todos, that's the route. Okay, so, then it will run this. So again, this todosRouter, it can do everything app can do.
[00:05:19] It can say, todos.use, todos.all, todos.params, todos.post, put, delete. It can do everything. You can also pass it in the middle right here, too, it's the exact same thing. As you can see, we get to separate different paths to different routers for different resources. Like, okay, so if we hit this resource, just load up this router.
[00:05:41] I mean, app doesn't even care what this thing does anymore. It's just like, you take care of it. I don't even care. But if app had some middleware up top here that stuff would still run through the middleware first before it comes to here. And then it would run through this thing's middleware and then it would go here.
[00:05:58] So, that's why I said global app controls global middleware. So, if there was more middleware up here, it would still go through there first, unless we assigned this app.use before the middleware. Right, so if we put app.use/todosRouter and then we put apps middleware underneath it, then it would never go through that middleware, it would only go straight to the todosRouter, but if we put apps middleware up top up here then it would go through that middleware first and then come here.
[00:06:25] So it's all about positioning. Just like the example I gave with the array and how all that stuff lines up, it's the same thing
>> Speaker 1: Any questions on the sub-routing? So again, we're going to start using the sub-routing, allows more flexibility, it has its own config, it's got its own stuff.
[00:06:49] This is actually really powerful. I was really excited about this. I almost gave up on Express a while back until they came out with this. I was like, finally. So this is a really, really great feature. But I guess you really wouldn't know it unless you were using Express 3 a lot.
[00:07:01] But this is a great feature, and so we're gonna use it