Middleware in Express.js is a fundamental concept that allows you to enhance and modify the request and response objects in the request-response cycle. It sits between the incoming HTTP request and the final route handler, enabling you to perform various tasks such as authentication, logging, validation, and more. Middleware functions are executed sequentially in the order they are defined in the application.
Here's a breakdown of how middleware works in Express.js:
Request Middleware: These functions are executed in the order they are defined when a request is received. They can perform tasks like parsing the request body, setting up authentication, or logging request details.
Route Handling: After request middleware, the request is passed to the appropriate route handler based on the URL and HTTP method.
Response Middleware: These functions can be used to modify the response before it's sent back to the client. For example, you can set headers, compress responses, or log response details.
Here's a simple example of using middleware in an Express.js application:
const express = require('express');
const app = express();
// Request Middleware
app.use((req, res, next) => {
console.log('Request received at:', new Date());
next(); // Pass control to the next middleware or route handler
});
// Route Handler
app.get('/', (req, res) => {
res.send('Hello, World!');
});
// Response Middleware
app.use((req, res, next) => {
console.log('Response sent at:', new Date());
next(); // Pass control to the next middleware (if any)
});
// Start the server
const port = 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
In this example:
The first middleware logs the timestamp when a request is received.
The route handler responds with "Hello, World!" when a GET request is made to the root ("/") URL.
The second middleware logs the timestamp when a response is sent.
When you make a request to this Express.js application, you'll see logs indicating when the request was received and when the response was sent.
Middleware is a powerful feature in Express.js, and you can create custom middleware functions to add specific functionality to your application. Middleware can also be used to handle error cases, perform authentication, validate input, and much more, making it a versatile tool for building robust web applications.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Middleware in Express.js is a fundamental concept that allows you to enhance and modify the request and response objects in the request-response cycle. It sits between the incoming HTTP request and the final route handler, enabling you to perform various tasks such as authentication, logging, validation, and more. Middleware functions are executed sequentially in the order they are defined in the application.
Here's a breakdown of how middleware works in Express.js:
Here's a simple example of using middleware in an Express.js application:
In this example:
When you make a request to this Express.js application, you'll see logs indicating when the request was received and when the response was sent.
Middleware is a powerful feature in Express.js, and you can create custom middleware functions to add specific functionality to your application. Middleware can also be used to handle error cases, perform authentication, validate input, and much more, making it a versatile tool for building robust web applications.