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 (using Express & Mongo) course:
The "Exercise 3" Lesson is part of the full, API Design in Node.js (using Express & Mongo) course featured in this preview video. Here's what you'd learn in this lesson:

In this exercise, you will add a number of middleware methods to the server. The middleware will handle incoming lion requests, errors, and additional routing.

Get Unlimited Access Now

Transcript from the "Exercise 3" Lesson

>> [MUSIC]

>> Speaker 1: All right. So, now what we'll do is check out the step three.
>> Speaker 1: And [SOUND]
>> Speaker 1: I gotta do a stash.
>> Speaker 1: There we go. Yeah, if you check out the step three, and go into server, server js, it's got a lot of stuff going on in here.

[00:00:32] So, your objective is to use a new method on the app called params. So using app.params, we're going to abstract some common functionality we've been doing this whole time. So if you notice on our routes, every route that has ID property, we had to find it to do with that id.

[00:00:52] So, bit of piece of common functionality. We can use a middle ware function to do that first, before it gets to that route. So, by time that route runs, we can just access request.lion and get the appropriate lion without having to look for it. So, that's what we'll do with app.params.

>> Speaker 1: And I'll show you an example of that. Actually, is it, yeah, that should be params, or is it param? Hold on, let me look. Don't even know. App dot, it's param, it's param. No pluralize, okay? So you'll use app.param here, which will look for anything. It will, this function's gonna run if it detects a URL that's using a query parameter with the given name.

[00:01:43] So, if we hit the URL that has slash ID, it's gonna run this cuz it's ID. If I change this name to thing, then this will never run cuz we don't have a slash thing here. But if I said, yeah, now we do. Then that will run.
>> Speaker 2: So, the param method, that's built into Express.

>> Speaker 1: Yes.
>> Speaker 2: It's wrapping around use, right?
>> Speaker 1: It's-
>> Speaker 2: I mean, it's adding middleware, right?
>> Speaker 1: It's adding middleware, that's exactly what it's doing. It's adding middleware. So what it does, it gives you the request, the response, the next, and then it gives you the value of that ID as a fourth argument right there.

[00:02:25] So it gives you the value. So what we can do inside of here is fill this out and find the lion based. So pretty much what we're doing, where were we doing this?
>> Speaker 2: Right there.
>> Speaker 1: I. So, I'm sorry. So, yeah, if you look at the last step we were doing in this app.getlions ID, we were finding the lion by that given ID in the would do that exact same thing.

[00:02:48] But here, and then we would attach the result of that to dot lion. So by time I got inside of here, if I counsel that log direct.lion, I will see the lion object.
>> Speaker 2: Yes? And that would apply to anyone that's got the ID program?
>> Speaker 1: Yeah, it would apply to any single one that's got the id property of it.

>> Speaker 2: That's really cool.
>> Speaker 1: Yeah, so by time I come in here if I just say what's rec.lion, it'll tell me. So this is a great place if you like database queries on a parameter. It's like, here's a user id. Let me find the user first before I pass it off to the actual function, and attach it to the request object.

[00:03:20] Cuz it's just an object. So attach it to the request object, and remember to call next. So that's one thing you'll do. The next thing you'll do is gonna create a middleware function to catch and handle errors. Register it as the last middleware in the app. So I've showed you how to do that, but make sure we have that, catch and handle errors.

[00:03:38] And then, third thing you're going to do is create a route middleware for the post lions that will increment and add an ID to the incoming new lion object to direct body. So what I mean by that is, if you go look at the post
>> Speaker 1: this one right here.

[00:03:53] I took away the functionality of incrementing the ID, cuz post we increment the ID, instead of doing it here. Let's just make a piece of middleware, and I already gave it a name, updateId, which is here. So if you fill out updateId, you should have it increment the Id here.

>> Speaker 1: Right? And attach the actual update object or the new thing to the body. So if you look at rec.body inside of this function, you'll get the lion that we're posting to the server. It doesn't have ID property on it. Increment the ID right here on line 19, and add it to the rec.body, and then call next.

[00:04:36] So, therefore, by time you get to this post, I can say, yeah, the lion is rec.body. And I already know it has a ID property on it.
>> Speaker 1: So those are three things you're gonna do. And I noticed, I literally just told you the last, two minutes ago.

[00:04:51] I was gonna use this syntax for the middleware. But I'm using this syntax. So, if you want to change it, you can change it. But don't get freaked out if you see that it's not doing this, cuz it's only doing that. CUz I'm just returning, I'm not returning a function here, it's just the function itself.

>> Speaker 1: Cool. So, everybody do that, we'll have 30 minutes and then we'll get back. There was on quick question. Yes?
>> Speaker 2: Can you, why would you recommend Morgan versus Bunion for logging?
>> Speaker 1: Just cuz I just us Morgan, that's what I've always used. I think Bunyan's awesome, too.

[00:05:25] It's just, Morgan worked for me, so I just kept using it. I don't recommend either way. It's just what I've used. So if you wanna use Bunyan use it. It might even be better.
>> Speaker 2: ID is hiding the other ID variable in the closure. Well, what's up with that?

[00:05:43] Is hiding the other ID variable.
>> Speaker 1: I'm not too sure exactly what it's saying, but let me see. The ID that we pass in here, it's hiding the other id enclosure. This one. I see, I see. Yeah, because the names, yeah, so, the ID here is not referencing the ID outside of here.

>> Speaker 1: Which is fine because we're not incrementing the outside ID in this param anyway. So it's fine. But if you wanna call it something else, you can totally, you can call it whatever you want here. Other ID if that makes you feel better.