Middleware in Express.js
Middleware is one of the most powerful and important concepts in Express.js. It allows you to execute code during the request-response cycle before sending the final response to the client.
In simple terms, middleware acts as a bridge between the incoming request and the final response. It can modify the request, process data, perform validation, or even stop the request if needed.
What is Middleware?
Middleware is a function that has access to three main objects:
req– The request object contains all the information about the incoming HTTP request, such as URL, parameters, query data, headers, and body sent by the client.res– The response object is used to send a response back to the client. It allows you to return data, JSON, HTML, status codes, and headers.next– The next function is used to pass control to the next middleware function in the stack. It ensures that the request continues to the next step instead of stopping.
A middleware function must call next() to pass control to the next middleware or route handler.
If next() is not called, the request will hang and no response will be sent.
Basic Middleware Example
In this example:
app.use()registers middleware- The function runs for every incoming request
next()passes control to the next handler
Middleware (printed in console for every request)
How Middleware Works
Middleware functions are executed in sequence, one after another. Each middleware can perform some operation and then decide whether to pass control to the next one.
Flow:
- Client sends request
- Middleware runs
- Next middleware runs
- Route handler sends response
Types of Middleware
1. Application-Level Middleware
These are defined using app.use() and apply to all routes or specific routes.
2. Route-Level Middleware
These apply only to specific routes.
3. Built-in Middleware
Express provides built-in middleware like:
express.json()– parse JSON dataexpress.urlencoded()– parse form dataexpress.static()– serve static files
4. Third-Party Middleware
Middleware created by external libraries, such as:
- morgan (logging)
- cors (cross-origin requests)
- helmet (security)
5. Error-Handling Middleware
Special middleware used to handle errors. It has four parameters:
Real-World Use Cases
- Authentication and authorization
- Logging requests
- Validating user input
- Parsing request body
- Handling errors globally
Why Middleware is Important
Middleware helps keep your code clean and modular. Instead of writing the same logic in every route, you can reuse middleware functions across your application.
It also improves scalability by separating concerns like authentication, validation, and logging.
Common Mistakes
- Forgetting to call
next() - Sending multiple responses
- Placing middleware in the wrong order
Best Practices
- Keep middleware small and focused
- Use separate files for middleware
- Place middleware in correct order
- Handle errors properly
Conclusion
Middleware is a core concept in Express.js that allows you to process requests before sending a response. It provides flexibility, modularity, and control over how your application behaves.
Mastering middleware is essential for building scalable and maintainable backend applications using Express.js.

