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 "REST Routes with Express" 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:

Scott demonstrates how to pass in an exact match, a regular expression (regex) match, a parameter match, or a glob match to your REST routes using Express routing.

Get Unlimited Access Now

Transcript from the "REST Routes with Express" Lesson

>> Speaker 1: So now we're gonna get a little more into the REST routes with Express. So so far you've seen me use routes, you've tried to use some routes. Whether you understand them or not, now's the time that we're gonna actually dive deep into them and talk about them a little more.

[00:00:13] So basically Express was designed with REST in mind and pretty much has everything you need. So we know that when we build a rest API or it can be a combination of a route matching a verb, right? So like /user with a post or a get or a delete or whatever, or a put.

[00:00:29] So Express was designed with that in mind. So it makes it really easy to do those combinations of routes and verbs. So the matching system, as far as when it comes to routes, allows us to do a few things. So we can match on an exact route, like, literally slash whatever I put here exactly, we can match on a regex, we can match on a glob.

[00:00:52] If you don't know what a glob is, it's very much like a regex. We can also match on parameters. So just to give you some examples, let's just go through some of what that looks like, is I'm going to change this data one around. So I said you could match on the exact one, that's what we've been doing so far.

[00:01:09] So in this case, I'm matching exactly on /data. So we've done that one. And then I said you can match on, like, a regex. So if you were to type in, like, a regex here, I don't know, I want to start with let's say it has to start with me, I guess.

[00:01:30] Anything that starts with me, it'll match on this one right here. If you want it to match on glob for, like, instance, I would say /user and then anything after that, so as long as it has slash user and something after it, it'll match this request.
>> Speaker 1: You can also match on parameters, which is gonna be very useful.

[00:01:54] So if I were to say, this parameter, like, id, I can match on the parameter. And because the parameter is dynamic, you need some way of getting that parameter inside your controller, we'll talk about that later. But these are just all the different ways you can match routes.

[00:02:13] So an exact match, a regex match, a parameter match, a glob match. I'm sure there's other ones out there, but in my experience, the only ones you'll probably ever use are gonna be exact matching and parameter matching, especially for REST cuz that's just how REST works. REST is a resource and then sometimes a resource followed by an ID.

[00:02:37] And that's mostly REST, that's it. For special stuff, yeah, you can get fancy, do regex's and globs. If you wanna automate some things, and not have to register a route every single time yourself, then yeah, you could just say, well I know that there's a user resource, so I'm just gonna do that.

[00:02:53] And then I'm gonna deal with whatever comes after it pragmatically inside the controllers, versus letting express deal with it for me. Right, so if I just did this glob here, then I would have to figure out what's after here inside this controller or some middleware that I define myself.

[00:03:08] Whereas if I just did an exact match, Express will take care of it for me. So it really depends on what you want to do, how fancy you want to get. So that's route matching. I recommend just sticking to exact matching and parameter matching. Everything else is clever, but it's like people are going to hate you if you do it.

[00:03:32] Your team's going to hate you, so just don't do that. [LAUGH] It's very confusing if you do it the other way around. If you do globs or regex's. So, it also supports HTTP verbs on a route based level. So, together with the routing, the HTTP verbs together with the routing, you can create the REST APIs.

[00:03:53] So to give you some examples of that So we have app.get, we did So the get will help us, so we're talking about CRUD, right? So if we have create, read, update, and destroy, and we're talking about that. Let me get rid of this. There we go.

[00:04:07] So the get would help us with the R in CRUD, which is read, right? So for create we have a post, that would help us with the C, for U which is update we would use app.put, all right, this allows to put some data, so far we could say, I wanna put some data here.

>> Speaker 1: And get a request and response. The difference between put and post is literally nothing. They're both the same thing, they just have different names. They both do the same thing. You post some data to the server, it's just that the intention is different. A post is I'm creating something new.

[00:04:46] I'm creating a new resource. I'm creating a new user. I'm creating a new something. A put is I'm updating something. But they both send some type of data to the server. And expect some type of mutation on the database, and then they both expect some type of response that is, if they're creating something, they expect that new created thing to be sent back.

[00:05:06] If they're updating something, they expect the updating thing to be sent back, and that's what REST describes. This is why I it's blurry, because as you can see, I could just do whatever I wanted in here. So if REST says yeah, you've gotta do this, but I'm like yeah, screw it, I'm gonna do this instead, it kinda gets blurry.

[00:05:22] It's like well, what do you do? So yeah, that's just what REST describes, but you don't have to do it.
>> Speaker 1: Any questions on the put and the post? No, so the other one might be, so the D in delete would be app.delete. Like I said, Express words are very well with CRUD, so we see we got app.get, we got put, delete and post.

[00:05:47] And it handles all of our CRUD operations there.