Middleware and its advantages

Ram Gopal Varma Alluri
5 min readApr 2, 2020

--

What exactly is middleware?

Basically middleware is the function that is executed between the request and response cycle.

Middleware Lifecycle

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.

Example (Node.js)

creating our own middleware

So a middleware that appears first in the code, is executed before one that appears later. Now, you can think of the whole process like this, our request and response object that was created in the beginning go through each middleware where they are processed, or where just some other code is executed. 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 it's been piped from request to final response.

Note :

Express provides you with middleware to deal with the (incoming) data (object) in the body of the request . express.json() is a method inbuilt in express to recognize the incoming Request Object as a “JSON Object”. This method is called as a middleware in your application using the code "app.use(express.json());"

Summary

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(router is also a middleware)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.

Using 3rd party middleware

Let’s now use a third-party middleware function in order to make our development life a bit easier. We’re gonna use a middleware called Morgan which is a very popular logging middleware. So, a middleware that’s gonna allow us to see request data right in the console. So, we have this first middleware but again let’s use ours before that. So, now we call morgan and into this function, we can pass an argument that will kind of specify how we want the logging to look like. So, we can use some predefined strings for that and the one that I’m gonna use a predefined string called dev and actually you can see the different options in VS Code which are really smart and can give you the options that you can pass into this function.

let’s now go ahead and simply create some request.we have the information about the request that we just did.So, we get the HTTP method, we get the URL, we get the status code, the time it took to send back the response and also the size of the response in bytes. You could actually even save this log
to a file but that’s a bit too much for this small example, so this is more than enough. This is how we use third-party middleware from npm and there is a lot of middleware it’s actually quite nice, so you have this getting started with a couple of articles explaining the basics of Express and then you have a nice API reference. Check it out Express !!!

Param Middleware

let’s create a special type of middleware called param middleware. So param middleware is middleware that only runs for certain parameters, so basically,
when we have a certain parameter in our URL. Now in our example here, the only parameter that we might have in our route URL is the id and so we can write middleware that only runs when this id is present in the URL. It is quite simple actually. So here we specify first the parameter that we actually want to search for, so basically the parameter for which this middleware is gonna run, and it’s called id, and then our actual middleware function, as usual. We have access to the request and to the response object, and then senses a middleware function also to the next function which is mandetory

Advantages

  1. Optimization and better performance
  2. Can manipulate request object before reaching the server
  3. Can perform various functions on the request object
  4. Can improve client-side rendering performance
  5. Setting some specific HTTP headers
  6. Some logging functionality

--

--