The course is part of this learning path
In this course, you will learn how to create custom middleware in an Express application.
- Learn what middleware is and how it fits in the request/response cycle
- Learn how to implement custom middleware
- Learn about the next function and how it facilitates custom middleware
- Learn to use custom middleware in different ways to work with your routes
- Learn what happens when you do not include the next function as part of your middleware
- This course is intended for anyone who wants to learn about Express
Introduction to Express - Custom Middleware. Middleware is a function that executes between the request and response cycle as in in the middle of the cycle, while Express does have several built-in middleware functions that serve specific purposes. Express does offer the option to write custom middleware that fits your particular use case for your Express application. Transitioning over to the lab environment. Currently on the screen, I have an Express application that is running with a route that uses the .all method.
The .all method is used for all request methods sent to the specific route path. Because this route handles all request methods, what I'd like to accomplish through the use of custom middleware is that whenever a request is sent to this path, I can capture that method and log it to the console. To begin, I'm going to create a folder called middleware, and inside of that folder I will create a file called helpers.js. Next, inside the file, I will write a request method logger function for this middleware. const requestMethodLogger = arrow function and this arrow function will have three arguments; request, response, and next. Inside the arrow function I will console.log, using a template string, the request method.
After the console.log, I will execute next. Pausing for a moment, what is next? Next is an argument that references the next middleware function. When executed, it invokes the next middleware. What you may not realize is that Express routes and many Express features are just a series of middleware function calls working together to operate this application. Now I need to export this function. module exports = requestMethodLogger. This is ready, so I will go back to the server.js file. Now I need to import this middleware. const requestMethodLogger = require, and as our argument, the relative path to the helpers file that's inside of the middleware folder. Now, I need to use the middleware on the next line app.es requestMethodLogger. Now, this is ready to test out. On the right in Insomnia, I'm going to send three requests: a DELETE request, a GET request, and a POST request. Now in the terminal, there are three console.log messages matching with each corresponding requests. Now there is technically a drawback to how I approach setting up this middleware.
Right now instead of the app use statement, I didn't define a route path. Because I did not include a route path, every route within this Express application will execute this custom middleware. If I wanted to use this middleware for a specific route, like a hypothetical route called views, I just need to provide the route path as the first argument inside of the use statement. So, for demonstration purposes, I will now add /views as that first argument. Now this middleware will only work for the views route. There's also another approach to using this middleware for a specific route which does not require the inclusion of the app.use statement. Instead, I can include it as an argument for the specific route. I will comment out this app.use statement. In the route below, after the route path, I will add requestMethodLogger as the second argument before the arrow function. This middleware will now work just like before, but only for this route. To demonstrate this, over on the right side, I will send a POST request, and in the terminal there is a console.log message of POST. One last item for this video.
Going back to the helpers.js file, where I wrote the middleware, I want to demonstrate what happens if you do not include the next statement as part of this custom middleware. I will comment out the next execution, and over to the right, I will hit 'Send', and there's a problem; the request keeps going, I have to cancel the process. That is because the middleware does not end the request/response cycle. The JSON response is inside of the route and not the middleware, so it never occurs. So, as a general rule, if custom middleware does not end the request/response cycle, that middleware has to include next. And that's it, thanks for watching at Cloud Academy.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.