This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Middleware" Lesson
>> Scott: Okay, sweet, let's move on to talk about middleware. This one's gonna be quick because you pretty much already know what middleware is. I've been placing dimes and clues here and there, showing you what it is. Only you don't know is where to put it and how to call the next thing, but there's a function called next, so.
[00:00:20] Let's just talk about that. Middleware are functions that can be configured to run before the response is sent back, basically. Hey, before you execute this controller, run these functions in this order, that's middleware. And each function does something synchronously or asynchronously and passes control to the next function.
[00:00:37] You can think of it as a stack of hooks or functions that can call before the thing that you want to actually happen, actually happens. So you might have things that enhance the request object. You might have things that log, do authentication, all different types of things for the incoming request before it actually gets to where it wants to be.
[00:00:56] Great for analytics, great for authentication, all that stuff. So yeah, basically uses the callback function to pass control with the next function in the middleware stack. That callback function is called next. If we go look at these controllers that you've been writing, this function right here, it's just a callback function.
[00:01:14] The middleware way of writing code is not something unique to express. Middleware has been around for a long time. It's basically just, you have a stack of functions, right? You start at the first one. You execute that function, not before passing in a callback. That callback, when invoked, just calls the next function in a stack.
[00:01:33] That's all middleware is. It's like, here's a list of functions, an array of functions. Invoke the first one, pass in the call back to be notified when it's done. When it's done, go to the next one, call that one, and so forth and so forth, until there is nothing left.
[00:01:46] That's middleware. That's how we are able to do it. It's pretty simple in the implementation of it but it is very powerful in what it can do. So, like I said, it is great at authenticating, enhancement request, logging, can be mounted globally in a request or on a per route basis.
[00:02:04] There's nowhere you cannot put, nowhere, you can literally put it anywhere you want. When I say globally I'm talking about, you remember we have a global app, right? Inside of serverjs? Yeah, you see this global app? We have global middleware. When I say setting up middleware globally, that's what that means.
[00:02:26] That means it's on this app level. But you can also put it on a per-route level, I can put middleware here. I can also put it on the route definition for this, and the route definition for its children and so forth and so forth. I can even put it right on the controller if I want to, to be honest.
[00:02:41] It depends on what you wanna do. They're all the same. They all take the same signature, a request, a response, and then next. It's the same thing so it doesn't matter where you put it. So for instance, if wanted to, let's just do it right now. So I'm gonna run some middleware right now.
[00:02:55] I'm just gonna right something in line. I'm a make a function. Just gonna do this right there, and it's going to take in a request, response, and the next, all right? And all I'm going to do is just log the request,
>> Scott: I think request dot route? No, I'm not going to do that.
[00:03:14] I'm just going to say, hey from middleware. And then I'm gonna call next. This is important. And we'll see what happens if you don't call next. So I'm gonna do that and I'm gonna run this. I'm just going to just hit any API URL, cuz it's mounted at API, API/songs.
>> Scott: And if we go look at the server output, you'll see right there, hey from middleware, right?
>> Scott: See that?
>> Scott: Hey from middleware, right? So that's middleware, right? Now if I get rid of this next, what do you think is gonna happen?
>> Speaker 2: You get hung up.
>> Scott: This is gonna hang, yeah. Cuz it's like, what do I do now? I don't know what to do.
>> Speaker 2: It'll still log, right?
>> Scott: It'll still log, for sure, it's not gonna stop the logging. But it won't know what to do after it logs. See, it's just hung.
[00:04:17] If we go back it's just like, yeah, hey. Hey from middleware. What do you want me to do? It doesn't know what to do. It has no clue. The other thing about middleware is you can send errors. So if you pass an error here,
>> Scott: What do you think is going to happen?
>> Speaker 2: The error will actually continue on.
>> Scott: Crash server, the error will continue on?
>> Scott: Anybody want to just guess? What do you want to happen? Let me ask you that.
>> Speaker 2: Should be a error handling middleware that can.
>> Scott: There you go, exactly. So it's going to send this error somewhere.
[00:05:03] But eventually you gotta catch the error. It's not the holy grail, we still gotta catch our errors, even in Express. So you're right, you need to set up some error handling middleware that's eventually going to catch that error and respond appropriately. That's what that next for,so if the first argument you pass to next is an error.
[00:05:19] That's the only argument that it takes, it's an error. Nothing else goes into it. If it detects that it's like, cool, this is an error. Let me call your error handling middleware if you have one set up. If you don't have something set up it'll just crap the bed.
[00:05:31] So I don't think we have any set up in this example. No we don't, so let's try to invoke it and see what happens. So if we do that, boom, there you go. It literally just sent back the whole error. Here you go, figure it out, right? It just took a crap right on my console.
[00:05:52] So you don't wanna do that. So you wanna set up some error handling middleware, and that's what we're about to do now.