Custom Middlewares
Middleware functions are at the heart of Express.js. They are functions that have access to the request object (req
), the response object (res
), and the next middleware function in the application’s request-response cycle. This capability allows for building robust and scalable applications by enabling functionalities like logging, authentication, and error handling. In this blog, we'll explore how to create custom middlewares in Express.js, step by step.
Table of Contents
Introduction to Middleware
Setting Up Express Application
Creating Custom Middleware
Using Middleware in Routes
Chaining Multiple Middleware Functions
Error-Handling Middleware
Conclusion
Introduction to Middleware
Middleware functions can perform various tasks such as:
Executing any code.
Modifying the request and response objects.
Ending the request-response cycle.
Calling the next middleware in the stack.
If the current middleware does not end the request-response cycle, it must call next()
to pass control to the next middleware.
Setting Up Express Application
Before diving into creating custom middleware, let’s set up a basic Express application.
Step 1: Install Express
First, ensure you have Node.js installed. Then, create a new project and install Express:
Step 2: Create the Server File
Create a file named app.js
:
Run the application:
Your server should be running at http://localhost:3000
.
Creating Custom Middleware
Let's create a simple custom middleware that logs the request method and URL.
Step 1: Define the Middleware Function
Create a directory named middleware
and a file named logger.js
:
Step 2: Use the Middleware in the Application
In app.js
, require and use the logger middleware:
Now, every request to the server will be logged to the console.
Using Middleware in Routes
Middleware can also be applied to specific routes.
Step 1: Create Route-Specific Middleware
Modify app.js
:
Only requests to /about
will be logged by specificLogger
.
Chaining Multiple Middleware Functions
You can chain multiple middleware functions for a route.
Step 1: Create Additional Middleware
Create another middleware file auth.js
:
Step 2: Apply Multiple Middleware Functions
Modify app.js
:
Requests to /dashboard
will pass through both logger
and auth
middleware.
Error-Handling Middleware
Error-handling middleware is defined in the same way as other middleware, but with four arguments instead of three: (err, req, res, next)
.
Step 1: Create Error-Handling Middleware
Create a file errorHandler.js
:
Step 2: Use Error-Handling Middleware
Modify app.js
to use the error handler:
Requests to /error
will trigger the error handler.
Conclusion
Custom middlewares in Express.js are powerful tools for adding functionality to your applications. By creating and using custom middleware, you can handle logging, authentication, error handling, and more in a modular and reusable way. Middleware functions provide a clean and organized approach to manage the request-response cycle, making your codebase more maintainable and scalable.