Middleware is a function that runs during the request-response cycle. It has access to the request object req, the response object res, and the next function. Express passes each request through a chain of these functions, in the order you register them.
A middleware function can:
run any code, such as logging or timing
change req or res, for example by adding req.user
end the cycle by sending a response
call next() to hand control to the next function in the chain
// Custom middleware
app.use((req, res, next) => {
console.log('Time:', Date.now());
next(); // Call next to continue to the next middleware
});
This one logs a timestamp for every request and then calls next(), so the routes still run. A middleware that neither responds nor calls next() leaves the request hanging.
Rewriting in plainer words…
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.