Understanding Express Middleware{A beginners guide}
Tags: node, javascript, framework, express
Published: 2020-02-26 16:30:42

you’ve probably come across the phrase “Express Middleware ” countless number of times if you write Node.js code! Middleware functions as the name implies, are in the middle of the client's request and the server's response, they are executed in the middle of the request-response cycle. Express middleware is any function that is invoked by the express routing layer before your final request handler is made! ![source:Grewe, Lynne. 2019. "Express: Middleware." CS6320: SW Engineering of Web Based Systems, Cal State East Bay. Accessed 2019-02-27.](https://dev-to-uploads.s3.amazonaws.com/i/73eusy0bc095c9w8tstw.png)The middleware has access to the request and response objects and they can modify the request and response for things like adding authentication, request headers, parsing request bodies, handling errors and a whole lot of other useful & essential functionality to your Node.js applications. Alright, enough of all these technical jargon! I will give an example in clear “non-nerdy” terms! Otis tries to log in to his bank app to view his profile and know how much he’s been paid by Maeve after giving out advice{The client sends a request} before the server sends out his bank profile{response} he is required to be authenticated{middleware function}. The authentication, in this case, is a middleware function that must be executed before a response is sent out! If the middleware function that is being executed doesn’t end the request-response cycle it must call next () to allow the other middleware functions on the queue get executed!![code snippet](https://dev-to-uploads.s3.amazonaws.com/i/a6xv47sg888h915v6co5.png)From the snippet above we can note that the middleware is executed every time the application receives a request. This is because we didn't define a specific route the middleware function should run on! to do that, we simply pass the middleware only in the route we want it to get executed in as the second argument right after defining the route path!Middlewares can be declared to allow for reusability and to follow the DRY principle in cases where we would be carrying out the same function repeatedly!![Code snippet](https://dev-to-uploads.s3.amazonaws.com/i/evhcespo77zo81w17k8u.png)
There are various types of middleware functions and they all have various use cases!
APPLICATION-LEVEL MIDDLEWARE: This middleware function is bound to the instance of the app object by using app.use() or app.METHOD functions where method is the HTTP method of the request, The snippets above are all application-level middleware since they are bound to instances of the app object.
ROUTER-LEVEL MIDDLEWARE: it is very similar to the application level middleware except that it is bound to an instance of the express.router() function![Code snippet](https://dev-to-uploads.s3.amazonaws.com/i/h1lg7bo0tqcdise6i5hv.png)The documentation on the express website gives some pretty good examples to help you understand the topic https://expressjs.com/en/guide/using-middleware.html#middleware.router
ERROR-HANDLING MIDDLEWARE: ExpressJs has default error-handling parameters, these are error-handling functions defined in the same way we define normal Application/Router level middleware functions except that they have four arguments instead of three! The fourth one being the “error” arguments! Error-handling middleware must have this fourth function as it helps to identify it as an error-handling middleware.
THIRD-PARTY MIDDLEWARE: These middlewares are functions written by the rich ecosystem of javascript developers but can be imported into your code just as you do for other npm modules to add functionality to your express applications. An example is the body-parser module which is very popular with over 12 million weekly downloads. It is used to parse HTTP request bodies! Other third-party express middleware you can use for free can be found at https://expressjs.com/en/resources/middleware.html
In some cases, it is possible to have more than one middleware function in your application, it is very important to note that these functions are executed in the order in which they are written /included in your file!! ![Code snippet](https://dev-to-uploads.s3.amazonaws.com/i/kwb3aevaoznn7ar2h4x2.png)When the code in the snippet above is run, "start execution" gets logged first to the console then “Sweet in the middle” which is the middle function gets logged next and “end execution” gets logged last just as we expected!
Middleware functions are important in writing clean, functional & reusable code! I hope this article helps you better understand express middleware functions! For all the code examples above you could check this GitHub repo!
[https://github.com/Ghvstcode/Express-Middleware]
[Cover Image:"http://www.freepik.com"]
 OLUWATOBI!