This course has been updated! We now recommend you take the Introduction to Node.js, v3 course.

Check out a free preview of the full Introduction to Node.js, v2 course:
The "Dynamic Routes in Express" Lesson is part of the full, Introduction to Node.js, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Scott implements dynamic routing in Express to access specific TODO items. The deprecation of the body-parser module is also covered in this segment.

Get Unlimited Access Now

Transcript from the "Dynamic Routes in Express" Lesson

[00:00:00]
>> Well, I know everyone was excited to get to the API's and stuff so I kind of wanna leave a little information about how you can go further with this. Cuz I would imagine this is probably what you want to take advantage of. As far as like databases go I purposely didn't want to connect a database and do stuff like that.

[00:00:19] So I think it gets a little complicated ,but it's not much. It really depends on the ORM or the implementation of the database that you're using. That's going to determine how you use it in Express but for the most part, if you have an ORM that has models.

[00:00:35] Or whatever implementation that you have, you would just use those same things exactly what I'm using my database, some of these will just be async. And you can these handlers are async you can base in front of them, you can await them, and it works totally fine. So you can do all the things in here that are async.

[00:00:51] So this is where you would do your database stuff. Sometimes you will put them in middleware to, you might make your own custom middleware that checks the database. If the incoming requests API key actually exists or if it's valid, if it's not expired, you make middleware for that before it even gets here.

[00:01:05] So, you can pretty much do a lot of stuff with Express when it comes to building API's. And I mean, just the combinations have the routes here, just to give an example of another one that is probably something that you would wanna see is like a dynamic one.

[00:01:20] So for instance, if I wanted to be able to get it to do by a specific ID, I could do something like this. I can say app.get/todo/some parameter here called ID. And now I can say, well give me the to do or todos.find the to do who is to do.id triple equals rec.params.id.

[00:01:50] This is gonna be a string where I know this is a number, so I'm gonna convert this. To a number or I'll cover this to a number like that. I think that converts to a number. We're gonna find out. So look at the to do and then I'll just use them like this data todo.

[00:02:10] So now this is a dynamic parameter that can be anything but it's referred to as the id because that's what I called it here. If I called it something else, that's what it will be called here as well. So, we call this id and we'll close this one.

[00:02:24] And now if we go back here we start the server I got a missing a parentheses or am I missing? I don't know why that thing's freaking out on expected token, okay, maybe the plus sign doesn't like it. Let's try this. Okay, there we go. So we got that and now I need to post some to dues right quick so I can get some ids.

[00:02:56] Okay, cool, I got an id, and then now I'm gonna say, let me do a get to do slash that thing. You can go away and this thing freaks out because it says, I put to do so should be DB, let's try that again. Posts, get that Id, try that again.

[00:03:26] There we go. I was able to get that to do back by the ID with a dinette with a dynamic path. So you can see it gets really powerful. It just means that he's one in here. You can even do regex as in these passes he wants to Express is ridiculous.

[00:03:38] You should check out the documentation if you want to learn more about it. So there's no other questions about Express, we will move on. Unless there are some questions.
>> What might just use for the body parser, well, what's happened if we didn't include it?
>> Yeah, that's a good question.

[00:03:54] So, I'm gonna get rid of this one body parser json, and then I'm going to try to post. I'm gonna try to post and see what happens. I think that will give us the error that I'm looking for. So I got rid of the body parser and I'm gonna try to do a post here.

[00:04:10] And you can see right now, instead of it creating a todo that had an id and a text property, it only created an a todo with an id like why did it do that? Well, let's think about it. Req.body.text is now undefined ,there's nothing there. So if you add an undefined field to an object that basically just gets deleted when you turn it to json, so this this was basically undefined.

[00:04:37] And this was undefined because the body parser did this, the body parser takes whatever is posted on the body and attaches it to an object's called body. On the request object as an object, without the body parser, we wouldn't have this. And we would have to do the exact same thing we did here.

[00:04:57] We'd have to say request.on data, build the chunk, collect the chunk, and then we have it and that's just really gross. So we don't wanna do that. The body parser at least the body parser.json.that. URL encoded we're actually not using. But this does the exact same thing. But for query strings.

[00:05:16] So someone send up a URL with a query string that had, I don't know an object on it or a couple variables that are comma separated. The URL encoded will parse that and give us access to it via, where is it req.query, so it'd be something called req.query here, and it will give us access to that.

[00:05:35] So that's why we use the body parser. So, it's a very fundamental piece of middleware. In fact, a lot of this stuff used to be included in Express and then Express started to like decoupled was like there's going to be a little more fundamental and you can add whatever you want.

[00:05:50] So, that's how it is. But there's frameworks that sit on top of Express, because Express has become so universal that, pretty much every framework is built on top of Express now. So you'll see things like Nest JS, or I can't think of them, I don't really use them.

[00:06:06] But, most of the Graph QL stuff like Apollo, Graph QL Apollo Express a lot of stuff is built on top of Express. Because the pattern is so such fundamental to node these days. So yeah, you'll see a lot of stuff like that. Any questions?
>> This person says in their VS code it says body parser is deprecated.

[00:06:26]
>> Body parser is deprecated. It's hard to say why someone's VS code would say that I mean, the best way to confirm that is to go to body parser NPM. We could look it up, published two years ago, it very well may be deprecated. So we can go look at the GitHub, I don't see any deprecation notice here in the Docs or anything like that, but two years ago, it could have been.

[00:07:00] Who knows? I could look at some issues here. Yeah, it's hard to say why that would say that. But, I mean VS code is VS code. So, [LAUGH] It's not actually running your code is still a static analysis. So, if it works it works, VS code is just trying to be helpful before you actually attempt to run the code.

[00:07:21] So remember that it's not actually executing it.
>> Yeah, on stack overflow, just saying that the body parts are constructor is now deprecated and you needed to call your own code and dot json separately.
>> Yeah, yeah. Yeah, I remember that they did that a couple of years ago.

[00:07:38] It used to be the fact that you just had to use body parser and then you would pass in like json, true or extended, but now they got rid of that. And you actually just use these independently from each other. Good question. So is rest out and not mandatory.

[00:07:53] Well, actually we are doing req dot and you just aren't explicitly doing it. Why don't we do something like raise .json. That's actually ending it for us. It's basically saying, hey, I want to end this response with this request. That's got to be json, it's the equivalent of doing something like this, restaurant JSON is basically saying like res.end, but not before you do something like res., right head.

[00:08:18] You say, writeHead and then you put what is it content type, so it's like content type. Application, json, so res.json is equivalent of doing that, passing in a Json string here and then writing the head. So who wants to do all that from scratch, when you get to do res.json?

[00:08:44]
>> Could you compare Express and Hapi?
>> I can try, and that's only because I've only used Hapi once in my entire life. And what I can. So I'll give you the comparison that I know I can speak with the most experience about. And that would be that the community for Express is just much bigger.

[00:09:01] The support for Express is just much bigger. I don't know if Hapi is better or more performant or overall in general just you should use it. All I can say is that Express sis has the most support the most plugins the most middleware, the most framework sitting on top of it.

[00:09:18] Probably the most active community of any server framework and node js. And I can't say that about any other framework even Hapi, although Hapi is obviously very well known and there's a big framework. It's just not as big as Express. It's like I guess the difference between like Vue and react they're both amazing I love Vue.

[00:09:38] But reacts communities is overwhelmingly huge compared to Vue even though Vue's community is pretty big too. So, it really just depends. On what you're looking for, you can't go wrong with either the support there for both it's pretty great.
>> Then someone's saying a body parser is actually built into Express with V5.

[00:09:59] And so you can just do app.useexpress.json.
>> Yeah, I have yet to use Express version 5. I haven't even looked at it. Last time I checked it out they were still working on it. So, it sounds like maybe they haven't figured out I know that the final version or if you're looking at a beta, but I have to check that out.

[00:10:13] That's pretty cool that they, cuz I mean I would literally just said, hey, everyone uses body parts. I don't know why they just don't include this. So, the fact that they did it, it's pretty impressive, but yeah, I've been. I'm on the 4.x, the 4.x train right now.

[00:10:26] Rocking that Express 4, so yeah, I would check that out good to know thanks.
>> I'd recommend that people check out your API design in node course if they wanna go further.
>> Yeah, definitely recommend checking that one out. It's like up to V3 now, and I go pretty deep on building API's with Express and node and all those using the Express version 4.

[00:10:50] It's pretty much the same thing. So, highly recommend checking that one out and if you like graph QL that's what you're doing. I also have a back end graph QL course on Friday masters as well if you wanna check that out, and that sits on top of Express, so, it's just express all the way top to bottom.

[00:11:05] There's no way around it. I promise you, they're not paying me to say this. Just like Express is a good tool. So, yeah.