API Design in Node.js (using Express & Mongo) What is Middleware
This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "What is Middleware" Lesson
>> Speaker 1: So how does everybody feel about the last exercise we did as far as going through this blueprint and actually making the routes? Now if you didn't get finished that's totally fine. I'm not so worried about if you actually finished it as much as I am with you actually understanding what you had to do and what needed to be done, right?
[00:00:23] So if you knew the gist of, yeah I need to make a route that does this. I'm just having a hard time messing around with the syntax of, or the nuances of the framework, that's fine. I mean, that can be resolved easily by me coming around, or a quick search.
[00:00:36] But the hard thing to know is what you're actually doing. That's the part that I'm trying to teach you.
>> Speaker 1: So this process that I'm showing you right here is a very similar process on a very small scale of how you would design an API, right? So start with the resource, start with the routes to access the resource and what they do.
[00:00:56] And then go ahead and implement that. That's like a very low level. And this is, it's super low level because we don't have a database or anything like that. But even with a database, it's very similar to this.
>> Speaker 1: Cool. Any questions on this stuff before we move on?
>> Speaker 2: One quick one, and you don't have to spend a lot of time on it. Any just thoughts on naming of things or plurals versus singulars and stuff, or guidelines to follow?
>> Speaker 1: Are you talking about routes?
>> Speaker 2: Yeah.
>> Speaker 1: Yeah, I haven't read the 50 or 100 spec on [INAUDIBLE] but I'm pretty sure that if you follow the rest of the the resources should be pluralized.
>> Speaker 2: Okay.
>> Speaker 1: Yeah, pretty sure.
>> Speaker 2: And is there, and for someone that's a little bit more green in this area, is there some reference that you recommend? Or does anyone know, if they're listening to the stream, do you want to pop it on chat, that'd be awesome.
>> Speaker 1: For RESTful stuff?
>> Speaker 2: Yeah, just in general
>> Speaker 1: There's endless stuff. I couldn't recommend one that I felt comfortable, there's so much stuff out there. But just start reading.
>> Speaker 2: Yeah.
>> Speaker 1: You just gotta find one that, I mean, if I Google it, it'll be 1,000 pages of
>> Speaker 2: No problem.
>> Speaker 1: Yeah.
>> Speaker 2: Thanks
>> Speaker 1: Okay, so now we're gonna talk about middleware.
>> Speaker 1: Anybody want to tell me their definition of middleware? I kinda got into it earlier, but don't look at it. Tell me what you think it is.
>> Speaker 1: Nobody? Yes. What is it?
>> Speaker 2: Kind of like utility classes to help Express do the dirty work.
>> Speaker 1: I like that. A set of utility classes help Express with dirty work. I like it. Anybody else?
>> Speaker 1: No, okay. So, what you were describing is what middleware would do. That's pretty much exactly right.
[00:02:49] It's definitely gonna help Express do some dirty work. But middleware is the backbone of Express. So I was telling you Express is really just two things. It's a routing framework that allows us to have advanced, really what it does is it sits on top of the HTTP module and it sets up these events.
[00:03:08] Because Node has this eventing system. The HTTP modules fires off events depending on what requests comes in. Express listens to those events and registers callbacks that fire to specific routes that are used to regexes. And then for each route that's registered, it'll keep a stack of middleware. Which is just an array of functions that it will call in order before it calls the last function.
[00:03:33] So, middleware, when you hear me say middleware, it's just a function. That's all it is, it's just a function. When I say middleware, I'm talking about one function. So, middleware is a function with access to the request object, the response object, and the next function. So, the next function is a function that when called will go to the next middleware.
[00:03:55] That's why it's referenced as next. So, you'll do some stuff in a function, maybe you'll change the request object, maybe you'll delete this, maybe you'll query the database here. And then when you're done, you'll say next, and it'll go to the next function in the middleware. Which could be the last function, or another piece of middleware that you created.
[00:04:12] So to
>> Speaker 1: so in our example, this right here, so this is global middleware. So express.static is a function that returns another function. So, like I said, this is a function that returns another function. So, like this. Var results.
>> Speaker 1: This returns another function. So, we're putting result in here.
[00:04:39] That's what's happening, like this. And then result is a function that looks like this. It kinda looks like this.
>> Speaker 1: It takes in a request, a response, and then next. That's what result looks like. And then it'll do some stuff. And then when it's done, it'll say next.
[00:04:59] And then when it calls next, it'll go to this one. And this will do the exact same thing. It'll do some stuff and then it'll go next. And this'll do the exact same thing, and it'll do whatever it needs to do its job, and then it will say next.
[00:05:14] And it'll come down to our routing. And then whatever route that actually came in, it'll hit that one. So when it says next, okay, now I'm gonna go here to this piece. Yeah?
>> Speaker 2: So is it just like a series of callbacks that we chain together?
>> Speaker 1: That's exact, that's all it is.
[00:05:30] It's just a stack of functions.
>> Speaker 2: Gotcha.
>> Speaker 1: And it won't go to the next one until you tell it to.
>> Speaker 2: Gotcha.
>> Speaker 1: So that's pretty much what middleware is.
>> Speaker 1: So middle wear can run any code. So, like i said, it's just a function. So it can run any code that you could run anywhere else.
[00:05:50] So, a database call. You make a call to another server. Maybe even send a response back to the client. It can do anything that we did in any of these call backs here. It's just a function. We can do whatever we want. That's why we have this middleware that, oops, let me go back.
>> Speaker 1: That's why we have this middleware up here that we downloaded, or expressed our static, which serves a static assets for us, or body parts that encodes the URL. Or bodyParser.json that takes everything and converts it to JSON and puts it on the body for us. It can do whatever it wants, as long as it follows a few simple rules.
[00:06:29] So it can do whatever it wants. But as far as changing the request object, the the response object, but it at least must end the request response cycle, or it must call next. If it doesn't do one of those, then your service is gonna hang. So if you don't believe me, I'm gonna show you.
[00:06:48] So I'm gonna say app.use. I'm just gonna make some middleware right here. It takes in a request, a response, and then next. And what I'm gonna do is I'm gonna say console.log.
>> Speaker 1: First middleware.
>> Speaker 1: All right. So, I'm gonna start the server.
>> Speaker 1: So, port 3000. So, soon as I make a request to it, it's gonna run the middleware.
[00:07:25] So this is gonna make a request on port 3000. It's gonna refresh, okay. So you can see it's hanging. It's still loading. If I go back to the terminal
>> Speaker 1: first middleware logs. It didn't go anywhere else. It's just sitting there. Cuz I didn't say next, or I didn't do a response.
[00:07:41] So it's just like, I'm just gonna sit here until I time out. All right? So, you have to make sure you do that inside your middleware. So to fix that, I could just say next, which will go here. Or I can just send back a response back to the client, and it'll be like, yeah, okay, yeah, yeah, yeah.
[00:07:58] And end the cycle, which would not go to next. So, everybody follow me there? Yeah?
>> Speaker 2: Can you set middleware after a route, or is it done once you hit that routing step?
>> Speaker 1: Can you set middleware after-
>> Speaker 2: If you clean up after a route or something like that.
>> Speaker 1: You can set, as long as you, so all these routes here are actually middleware, too. So all these have a next function, too.
>> Speaker 1: Every route that we have, we just didn't use them. There's a next function here. So when you call next inside this route, it will also go to another middleware unless you respond it.
[00:08:37] The reason we didn't put next here is cuz we were responding. So we couldn't say, yeah, respond and then go to next.
>> Speaker 1: Cuz actually two things are gonna happen. One, there is no next. There's nothing after this in the middleware chain. And two, we already responded. So it's just gonna break.
>> Speaker 1: But, yeah, you could theoretically. So if you had this route set up and you wanted to go somewhere after this, you just need not to respond. But then technically this wouldn't be a route handler, it'd just be middleware, right? We're gonna get into how to use middleware in different places and controller methods.
[00:09:17] But yeah, you could. These are all the same thing. As long as you call next, it will go to the next thing in the stack.