What is middleware?
In Express.js, middleware refers to a set of functions that are executed in a sequential order during the request-response cycle of an application. These functions have access to the request and response objects, allowing them to perform tasks such as parsing request parameters, handling authentication, logging, error handling, and much more.
How does middleware work in Express.js?
When a request is received by an Express.js application, it goes through a series of middleware functions before the final route handler is executed. Each middleware function can modify the request and response objects or pass control to the next middleware in the stack using the next()
function.
Middleware functions can also perform error handling by calling the next()
function with an error object as an argument. This allows for centralized error handling and avoids repetitive error handling in individual routes.
Creating and using middleware in Express.js
To create a middleware function in Express.js, you simply need to define a function that takes three parameters: req
(the request object), res
(the response object), and next
(the next function in the middleware stack).
const express = require('express');
const app = express();
// Custom Middleware function
const logger = (req, res, next) => {
console.log('Request received at:', new Date());
next(); // Pass control to the next middleware
};
// Using the middleware function
app.use(logger);
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
In the above example, the logger
middleware logs the date and time at which a request is received. app.use(logger)
adds the middleware function to the application, ensuring that it is executed for each incoming request.
It’s important to note that middleware functions can be used globally using app.use()
or locally for specific routes using router.use()
. This allows for fine-grained control over the middleware execution.
Conclusion
Middleware plays a vital role in Express.js by allowing developers to handle various tasks during the request-response cycle. Whether it’s parsing request data, handling authentication, or performing error handling, middleware provides a flexible and modular approach to extending the functionality of an Express.js application.
#expressjs #middleware