Middleware is a function that has access to the request object, response object, and the next function which points to the next middleware. Since the request and response object are available, middleware is able to change these objects, run any code, or even end the request/response cycle.
(upbeat music) - 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 as you understanding what you had to do, and what needed to be done. Alright, 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 "or the nuances of the framework." That's fine. That can be resolved easily by me coming around or a quick search.
But, the hard thing to know is what you're actually doing. That's the part that I'm trying to teach you. So this process that I'm showing you right here is like a very similar process on a very small scale of how you would design an API. Alright, so start with the resource. Start with the routes to access the resource and what they do. And then go ahead and implement that. That's like a very low level. And this is 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.
Cool. Any questions before we start to move on? - [Student] One quick one and you don't have to spend a lot of time on it. Any thoughts on, you know, naming of things or plurals versus singulars and stuff or guidelines to follow? - Are you talking about, like, routes? - [Student] Yeah. - Yeah. Um, I'm not, I haven't read the 50 or 100 page spec. on REST, but I'm pretty sure that if you follow REST to the T, the resources should be pluralized. - [Student] Okay. - Yeah, I'm pretty sure. - [Student] So and is there any, and for someone that's a little more green in this area, is there some reference that you'd recommend? Or does anyone know if they're listening to the stream? If you want to pop it on chat that'd be awesome.
- For RESTful stuff? - [Student] Yeah. Just in general. - Oh, there's endless stuff. I couldn't recommend a one that I feel comfortable with because there's so much stuff out there. - [Student] Yep. - [Student] Yeah. Start reading. - Yeah. You've just got to find one that... Cause if I googled it, it would be a thousand pages alone. - [Student] No problem. Thanks. - Okay, so now we're going to talk about Middleware. Anybody want to tell me their definition of Middleware? I kind of got into it earlier, but don't look at it.
Tell me what you think it is. Dun dun dun. Nobody? Yo, yes what is it? - [Student] Kind of like a set of utility classes to help Express do the dirty work? - I like that. A set of utility classes to help Express do the dirty work. I like it. Anybody else? Nope? Okay. So what you were describing is what Middleware would do. That's pretty much exactly right. It's definitely going to help Express do some dirty work. But, Middleware is the backbone of Express.
So I was telling you that Express is really just two things. It's a routing framework that allows us to have like advance... really what it does is it sits on top of the HTTP Module and it sets up these events because Node has like this eventing system. The HTTP Module fires off these events, depending on what request comes in, Express listens to those events then registers fall-backs that fire to specific routes that they use for regex's. And then for each route that's registered it will keep a stack of Middleware.
Which is just an array of functions that it will call in order before it calls the last function. 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. 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 you'll go to the next function in the Middleware. Which could be the last function, or another piece of Middleware that you created. 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 (typing) var.results.
(typing) This returns another function. So we're putting results in here. That's what happening like this. And then results is a function that looks like this. It kind of looks like this. (typing) 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. 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 it'll go next. And this will do the exact same thing. It'll do whatever it needs to do it's job, and then it will say next. And then it will come down to our routing. And whatever route that actually came in, it'll hit that one. So when it says next, it's like "okay now I'm going to go here... "to this piece." Yeah. - [Student] So is it just like a series of call-backs that we chain together? - That's exactly, that's all it is. It's just a stack of functions. - [Student] Gotcha. And it won't go to the next one until you tell it to. - [Student] Gotcha.
- So that's pretty much what Middleware is. (mouse clicking) So Middleware can run any code, so like I said it's just a function. So it can run any code that you can run anywhere else. So, a database call can 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... Whoops, let me go back.
That's why we have this Middleware up here. That we downloaded for express.static, which serves the static access for us, or bodyParser, that encodes the URL. Or, bodyParder.json that takes everything and converts it to json and puts it on the body for us. It could do whatever it wants, as long as it follows a few simple rules. So it can do whatever it wants, but like as far as changing the request object, 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 going to hang. So if you don't believe me, I'ma show ya. So I'm going to say app.use. (typing) I'm just going to make some Middleware right here. It's takes in a request, a response, and then next. And then what I'm going to do is I'm going to say console.log (typing) first middleware. (typing) Alright, so I'm going to start the server.
(typing) It's on port 3000, so as soon as I make a request to it it's going to run the Middleware. So this is going to make a request on port 3000. Just going to refresh. Okay. So you can see it's hanging. It's still loading. If I go back to the terminal, first middleware logs. It didn't go anywhere else. It's just sitting there. Cause I didn't say next, or I didn't do a response. So it's like "I'm just going to sit here until I time out." Alright, so you have to make sure you do that inside your Middleware.
So to fix that, I can just say next, which will go here or I can just send back a response back to the client, and be like "oh yeah, okay, yeah." And end the cycle. Which would not go to next. Does everybody following me there? Yeah. - [Student] Can you set Middleware after a route, or it's done once you hit that routing step? - Uh, can you set Middleware after? - [Student] Like you clean up after a route or something like that. - You can set, as long as you, so all these routes here all actually are Middleware too.
So all these have the next function too. Every route that we have, we just didn't use them. There's a next function here. So when you call next inside of this route, it will also go to another Middleware. Unless you, unless you responded. The reason we didn't put next here, is cause we were responding. So we couldn't say "oh yeah respond and then go to next." Cause actually two things are going to happen. One, there is no next. There's nothing after this in the Middleware chain.
And, two, we already responded. So, this is going to break. But, yeah you could, theoretically. So if you had this route set up, and you wanted it 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 going to get into, like, how to use Middleware in different places, and controller methods. 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. (typing)
This course was created by Frontend Masters. It was originally released on 12/30/2015. We're pleased to host this training in our library.
- Executing Node.js
- Using Express
- What is middleware?
- Testing in Node.js
- Using Mongo with Node.js
- Data modeling
- Querying data with Mongoose
- Identifying sensitive routes
- Configuring the deployment