Lesson 7 / 23

What is Middleware?

Functions that run between the request arriving and the response going out.

req, res, next

A middleware is a function (req, res, next) => {} that can inspect or modify the request/response, then call next() to pass control along — or end the response itself.

An airport security line

Each checkpoint (bag scan, ID check, boarding pass) inspects you and waves you through with next() — or stops you right there if something's wrong.

A logging middleware

app.use runs a middleware for every request, in the order it was registered.

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