What is middleware in Express.js?
Answer
Middleware in Express are functions that have access to the request (req), response (res), and a next function. They sit in the middleware pipeline between the incoming request and the final route handler. Middleware can execute code, modify req/res, end the request-response cycle, or call next() to pass control to the next middleware. Examples: logging every request, parsing JSON bodies, verifying authentication tokens, adding CORS headers. Register middleware with app.use(). Middleware functions run in the order they are defined, so ordering matters — for example, authentication middleware should run before protected route handlers.
Previous
How do you create a basic Express.js server?
Next
What is the difference between app.use() and app.get() in Express?