Node.js / Node.js Middleware

Chaining Multiple Middleware Functions

In this tutorial, we will delve into the concept of chaining middleware functions in Node.js. We will explore how to create a chain of middleware, how to pass control between midd…

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Covers the concept of middleware and its role in handling requests and responses.

1. Introduction

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:

  • Create a chain of middleware functions
  • Pass control from one middleware function to the next
  • Structure your code to handle middleware chaining

Prerequisites:
- Basic knowledge of JavaScript
- Basic understanding of Node.js
- Node.js and npm installed on your machine

2. Step-by-Step Guide

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.

Best Practices and Tips

  • Always keep your middleware functions small and focused on doing one thing. This makes it easier to debug and maintain your code.
  • Always call 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.

3. Code Examples

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.

4. Summary

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.

5. Practice Exercises

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!.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

Random String Generator

Generate random alphanumeric strings for API keys or unique IDs.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help