Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Introduction to Middleware | Section
Building APIs with Express.js

bookIntroduction to Middleware

Swipe to show menu

Middleware is a function that runs between receiving a request and sending a response.

It has access to the request, the response, and a special function called next.

app.use((req, res, next) => {
  console.log('Request received');
  next();
});

When a request comes in, middleware runs first. After it finishes, it calls next() to pass control to the next step.

The flow looks like this:

Request → Middleware → Route → Response

Middleware is used to process requests before they reach your route logic.

Common use cases include:

  • Logging Requests;
  • Parsing Data;
  • Checking Authentication.
question mark

What does next() do in middleware?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 10

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 1. Chapter 10
some-alt