Middleware in Express is a function with access to the request, response, and next() that acts as a checkpoint between an incoming request and the final route handler. It covers application-level, router-level, and built-in middleware, execution order and why it matters, how next() moves the request forward or triggers error handling, and practical examples like logging, authentication, and request validation.
Questions this post answers
What happens if I forget to call next() in an Express middleware function?
The request hangs indefinitely because Express has no way of knowing to move forward, and no response is ever sent back to the client. The client typically experiences this as the request timing out. Calling next() is required to pass control to the next middleware or route handler in the chain. Debugging a hung Express request often comes down to a missing next() call worth tracking on daily.dev.
Can Express middleware modify the request object before it reaches the route handler?
Yes, middleware can attach data to the req object, such as a decoded user object after verifying authentication, and the route handler can then read that data directly without redoing the work. This lets validation or auth logic run once and pass results downstream through the request object. Developers building auth flows in Express can compare middleware patterns like this on daily.dev.
Does the order in which I register middleware in Express matter?
Yes, Express runs middleware and route handlers in the exact order they are registered in the code, and getting this wrong is a common source of bugs. For example, if authentication middleware is registered after a route handler that needs it, that route runs completely unprotected, since order determines what protection or processing a request receives. Anyone structuring an Express app can revisit middleware ordering pitfalls on daily.dev.