
API Design in Node.js, v3 Router Verb Methods
This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Router Verb Methods" Lesson
[00:00:00]
>> Scott Moss: So one last thing I wanted to show you with the router in the app is we talked about the verbs like get, post, use, put, delete. All that's really cool, but when you're doing res, they're all the same thing. If we think about res, there's literally the exact same routes.
[00:00:20] Every single time. No matter what resource you have. I'm just gonna go through some examples. So I'm just gonna say routes. Just to show you how. So we'll have, let's say our resources, let's say we have a pet app, and our resource is cats. Okay, so we got cats here.
[00:00:38] I like cats. So if we wanted to do a get request to get one cat, it'll be /cat, right? If we wanted to do, let me get that cat with that specific ID it'll be, /cat: id, all right? But those are two get requests. Let's just put this like this, get, get.
[00:01:01] If I wanted to create a cat, it would be a post request to /cat.
>> Scott Moss: If I wanted to update a cat, it would be a put request to /cat with an ID of the cat that I wanna update. And then if I wanted to remove a cat because it got adopted, then I would put delete a cat with the ID of the cat.
[00:01:25] And that's never gonna change for res. It doesn't matter what your resource is, it's always gonna be these five different routes, right? If you follow res it'll be these five. That's why I say it's really blurry because it's really hard to build an application that only does this.
[00:01:40] Sometimes you need more, like there's different relationships. This cat might have an owner that has this thing. It gets complicated. But if you just follow res, these are the only routes you'll ever need. This is why Rails was so popular cuz they were able to scaffold this stuff out for you.
[00:01:52] Cuz it was like, this is it, this is all you'll ever need. So if that's the case, it's kind of tedious to write those over and over and over and over again especially if we look at some of these routes. So if we look at the cat resource, there's only two routes really.
[00:02:06] There's different verbs, get, post, put, delete. But look at the routes, there's only two, there's /cat and /cat:id, that's it. Those are the only two different routes. So, Express allows us to make this a lot simpler versus writing five different route combinations like this, we can just do it in two.
[00:02:23] So what you could do is using the router or the app, cuz they both have the same routing capabilities, doesn't matter. In this case I'll just say router. You can do .route, so .route allows you to pass any route, in this case I'll say /cat. And then from there, I can do my verbs.
[00:02:40] I can do .get, pass in my controller and my middleware. I can do .post, pass in my controller and my middleware, right? And then for the other route, I could say router.routes /cat/id. And then I could do my get, my put and my delete,
>> Scott Moss: Right? So that just cleaned up a lot of stuff for us just by doing that.
[00:03:10]
>> Scott Moss: And again, because a router and app have the same routing capabilities this works the same way on an app. So yeah, this is just a cleaner way to do it. So then now you just pass in your controllers and your middleware here just like we did down here.
[00:03:24]
>> Scott Moss: And that way you don't have to say router .get/cat, router .get /cat, cuz it gets very tedious. So this is just like yeah, it's res, just do this.