Middleware

View saved

What middleware is

A middleware function receives req, res, and next. It can end the response or call next() to continue.

Built-in JSON parser

Parse JSON bodies on POST/PUT routes.

app.use(express.json());

app.post("/echo", (req, res) => {
  res.json({ youSent: req.body });
});

Custom logger

Simple request logging for learning.

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

Order matters

Middleware runs in registration order. Put parsers and shared checks before the routes that need them.

Comments

One comment per signed-in account. Comments are saved with this page’s URL.