Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Creating Custom Middleware | Seksjon
Bygge API-er med Express.js

bookCreating Custom Middleware

Sveip for å vise menyen

You can create your own middleware to control how requests are processed.

A middleware function receives three arguments: req, res, and next.

const logger = (req, res, next) => {
  console.log(req.method, req.url);
  next();
};

app.use(logger);

This middleware logs the request method and URL for every incoming request.

When a request comes in:

  • Middleware runs first;
  • It performs its logic;
  • It calls next() to continue.

If next() is not called, the request will not move forward.

Custom middleware is useful for adding shared behavior across your application, such as logging or validation.

question mark

What happens if next() is not called in middleware?

Velg det helt riktige svaret

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 11

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 1. Kapittel 11
some-alt