This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Middleware Examples" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Scott Moss: So here's gonna be more examples of how to use it. So here's one piece of middleware that I highly recommend you take note of. You will be using it probably in the next step. Morgan, Morgan will log all the requests in the terminal as they come in So that's the 3rd party middleware.
[00:00:19] We MPM installed it and then we required it. Here's an arbitrary piece of middleware called checkAuth and we made it, you know we made it because there's a ./ in front of it, that means it's coming from our files and it's a piece of middleware that we made.
[00:00:34]
>> Scott Moss: So we globally set up morgan, app.use And then here's an example of like actually placing middleware on the route itself. So you can also do that. So this is application level, this is like a route level. Not exactly router level, we'll get into router level. But this is like a route level.
[00:00:55] So when I do get request to slice to do is, first run this middleware. And see what it does. This middleware can respond back to the client. Look like a 401 unauthorized array. Or you can say, yeah you're good, I checked the data base, next. And then come here.
[00:01:10] So we can put as many comma separated middleware as we want right here. We can also just pass in an array of middleware right here, too. And it would work the same way. It's pretty convenient. So the pattern. Yeah, you had a question?
>> Mark: So how do you stop the request response cycle?
[00:01:32]
>> Scott Moss: You either have to send a response. So rest.whatever.
>> Mark: Rest.send, yeah.
>> Scott Moss: Throw an error. Or, yeah, one of those two things. Yes?
>> Mark: So, if you are gonna cover it later just stop me, but the app.uses are procedural right? So if you have separate files and you break them out is it really load dependent on which one loads first that that will be the app.use that gets ran before each route?
[00:01:57]
>> Scott Moss: Good question. So, app.use is procedural and if you break them out in to different files, luckily for us, the require method is synchronous so whatever time you require that file. So let's say we put some app.users in here, right? We pull a few in here and then right underneath, maybe the third one, we require the file to other app.users and then all those will get ran and then the ones beneath that require will get ran.
[00:02:20] So it's all synchronous. So yeah, it's still gonna be in order, and luckily for us require is synchronous so yeah, that will still guarantee that. Good question.
>> Scott Moss: Any other questions on this stuff? On this example? Mark.
>> Mark: You might have answered this, but can you pass an array of functions as the second param of app.get?
[00:02:42]
>> Scott Moss: Yes, I answered that. I think there's a note right here that says it, too. Yeah, an array of Middleware. Yeah, so you can pass in an array of Middleware, too. One note though on this cuz this is kind of confusing. I said Middleware is a function. Actually, I'm just gonna show you.
[00:03:00] I think it's better just to show you. So right now the way the middleware, at least the third-party middleware, is written is that you notice that they're always invoked, like we're always executing the function, right. That's executed, that's execute it, this is executed it, right? But what value is that return in?
[00:03:17] Remember I said middleware is just a function, so if you were If you thought about it then you would know that these methods are just for returning middleware right, so this is a common pattern for us to pass an option to our middleware to expose our function right, these are our higher order function.
[00:03:32] I'll just show you, let's make our own so you can see what I'm talking about for options. It's a function that takes in some options. Okay. And what we want to be able to do is to, let me put it up top, what we want to be able to do is come down here and say app.use and then options and passes some options like this, like name.
[00:04:01] You want to do something like that. All right. So that's why this function takes in options, but because its middleware it must return a function. So that is why. This is where we would do it.
>> Scott Moss: So now this is the actual middleware function that's gonna be executed, right.
[00:04:21] So now I can come in here and say, next. But now I have access to the options that this person passed in, right, so this is high order function here. So that's why you'll see this a lot, but I'm telling you this because sometimes you might see it like this.
[00:04:40] Where it's just like 'options' with no implication. It's just like, that's my middleware. I don't want to throw you off your game but the reason that's there is because instead of having that. It's just like that instead, that's the only difference. So this doesn't return a function, this function is the middleware whereas before it was a function dot return the middleware function.
[00:05:11]
>> Scott Moss: So I recommend when you start writing your middleware, pick the pattern that you want and stick with it. I choose this pattern because most third party middleware ues the same pattern.
>> Mark: This pattern results from closures.
>> Scott Moss: Right, it's a closure.
>> Scott Moss: So just take note of that.
[00:05:35] So we'll use third party and built-in middleware a lot, and we'll also be creating our own middleware. So I'll just give an example of what creating our own middleware looks like. Here's another example. So, after I use, for instance, maybe you want to check to see if they supply the correct secret word before they went on, so you'll check the secret word, see if it's catnip, if it is catnip, next you may proceed.
[00:06:01] If it is not, set the status to a 401 and send a message that says no. Right? And then that will stop. That won't go into the next one. So it's simple stuff. So middleware can do whatever you want. This is a great place to do things. Any questions on middleware?
[00:06:20]
>> Scott Moss: So, what is middleware again?
>> Scott Moss: Anybody?
>> Student: It's like an array of callbacks?
>> Scott Moss: It's just a function. The middleware's just one function.
>> Student: Yeah.
>> Scott Moss: Yep. It's just one function. It's just a function.