‫In the ;ast couple of lectures,
‫I hope you started getting the fundamentals
‫of Express development.
‫And so now is the perfect time to dive a bit deeper
‫into how Express works,
‫and for that we need to talk about middleware
‫and the request-response cycle.
‫So really the essence of Express development
‫is to understand
‫and then use request-response cycle.
‫So I wanna make that idea really clear
‫in a visual way in this video.
‫So to start the request-response cycle,
‫or Express app receives a request when someone hits a server
‫for which it will then create a request
‫and response object.
‫That data will then be used and processed
‫in order to generate and send back a meaningful response.
‫Now, in order to process that data,
‫in Express we use something called middleware,
‫which can manipulate the request or the response object.
‫Or really execute any other code that we like.
‫So middleware doesn't always have to be just
‫about the request or the response object,
‫but it usually is mostly about the request.
‫And actually, we already used middleware before.
‫We used Express dot JSON to get access to the request body
‫on the request object.
‫Remember that?
‫Now it's called middleware remember
‫because it's a function that is executed between,
‫so in the middle of receiving the request
‫and sending the response.
‫And actually, we can say that in Express,
‫everything is middleware.
‫Even our route definitions.
‫So again, even when we defined our routes,
‫we can think of the route handler functions that we wrote
‫as middleware functions.
‫They are simply middleware functions
‫that are only executed for certain routes.
‫Okay?
‫Now some examples of middleware are Express dot JSON,
‫which is also called body-parser,
‫and that we already used before.
‫Or some logging functionality,
‫or setting some specific http headers.
‫The possibilities are really endless with middleware.
‫All right, and now in more technical terms,
‫we say that all the middleware together
‫that we use in our app,
‫is called the middleware Stack.
‫What's very important to keep in mind here,
‫is that the order of middleware in the stack,
‫is actually defined by the order they
‫are defined in the code.
‫So a middleware that appears first in the code,
‫is executed before one that appears later.
‫And so the order of the code matters a lot in Express.
‫Okay?
‫Now, you can think of the whole process like this,
‫our request and response object
‫that were created in the beginning
‫go through each middleware where they are processed,
‫or where just some other code is executed.
‫Then at the end of each middleware function,
‫a next function is called,
‫which is a function that we have access to
‫in each middleware function.
‫Just like the request and response objects.
‫And we will of course see this in code in the next video.
‫So when we call the next function,
‫the next middleware in the stack will be executed
‫with the exact same request and response object.
‫And that happens with all the middlewares
‫until we reach the last one.
‫And so just like this,
‫the initial request and response object
‫go through each middleware step by step.
‫And you can think of this whole process
‫as kind of a pipeline where our data go through,
‫so just like its been piped from request to final response.
‫All right?
‫Now about that last middleware function,
‫it's usually a route handler just like we coded before.
‫So in this handler we do actually not call the next function
‫to move to the next middleware.
‫Instead, we finally send the response data
‫back to the client.
‫And like this,
‫we then finish the so-called request-response cycle.
‫So the request-response cycle is really everything
‫that we talked about here together.
‫It starts with the incoming request,
‫then executing all the middleware in the middleware stack
‫step by step,
‫and finally sending the response to finish the cycle.
‫So you see, it's really not complicated.
‫It's actually just a linear process.
‫But I wish someone would have shown me something like this
‫when I was learning how to build Express apps.
‫I mean I could build them,
‫but in the beginning I didn't really understand
‫this entire request-response cycle like this.
‫It was very confusing.
‫So that's why I'm showing you this,
‫because I believe it will be much, much, easier for you
‫in order to move onto the course now,
‫after understanding exactly how Express apps work.