In this tutorial, we'll dive deep into chaining multiple middleware functions in Node.js. Middleware functions 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. The next middleware function is commonly denoted by a variable named next
.
By the end of this tutorial, you will learn how to:
Prerequisites:
- Basic knowledge of JavaScript
- Basic understanding of Node.js
- Node.js and npm installed on your machine
Middleware functions are a fundamental part of any Node.js application as they allow developers to manipulate the request and response objects, end the request-response cycle, or invoke the next middleware function in the stack.
When building complex applications, you may need to chain multiple middleware functions. Chaining middleware functions means that you call multiple middleware functions in a specific order, passing control from one middleware to the next.
next()
at the end of your middleware function if there are more middleware functions to execute. If you don't, your server will hang and the client will not receive a response.Let's dive into some code examples.
Example 1: Basic middleware chaining
const express = require('express');
const app = express();
// Middleware function 1
app.use((req, res, next) => {
console.log('Middleware function 1');
next();
});
// Middleware function 2
app.use((req, res, next) => {
console.log('Middleware function 2');
res.end();
});
app.listen(3000, () => console.log('Server is running on port 3000'));
In this example, we have two middleware functions. The first function logs a message to the console and then calls next()
, which passes control to the second middleware function. The second function logs another message and then ends the response with res.end()
.
Example 2: Chaining middleware with routing
const express = require('express');
const app = express();
// Middleware for /user/:id route
app.get('/user/:id',
(req, res, next) => {
console.log('ID:', req.params.id);
next();
},
(req, res, next) => {
res.send('User Info');
}
);
app.listen(3000, () => console.log('Server is running on port 3000'));
This example shows how middleware can be chained when defining routes. The first middleware function logs the user ID to the console and then calls next()
. The next middleware function sends a response to the client.
In this tutorial, we covered how to chain multiple middleware functions in Node.js. This is a powerful technique that can make your code more modular and easier to maintain.
For further learning, consider diving deeper into the Express.js documentation, particularly the sections on routing and middleware.
Exercise 1: Create an Express server with three middleware functions. The first function should log the current date and time to the console. The second function should add a X-Powered-By: Express
header to the response. The third function should end the response with a status code of 200 and a body of Hello, World!
.
Exercise 2: Modify the server from Exercise 1 to include a fourth middleware function that is only called for requests to the /admin
route. This function should log Admin page
to the console. Remember to call next()
in the first three functions and res.end()
in the last one.
Exercise 3: Modify the server from Exercise 2 to handle errors. If an error is passed to next()
, call a error-handling middleware function that sends a 500 response with the body Something went wrong!
.