Middleware in Express

Node js 10 min min read Updated: Mar 30, 2026 Intermediate
Middleware in Express
Intermediate Topic 3 of 10

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.

Key Concept: Middleware functions run between the request and response, allowing you to process or modify data before sending it to the client.

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

javascript app.use((req,res,next)=>{ console.log("Middleware"); next(); });

In this example:

  • app.use() registers middleware
  • The function runs for every incoming request
  • next() passes control to the next handler
Output

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:

  1. Client sends request
  2. Middleware runs
  3. Next middleware runs
  4. 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.

javascript app.use((req,res,next)=>{ console.log("App-level middleware"); next(); });

2. Route-Level Middleware

These apply only to specific routes.

javascript app.get("/user", (req,res,next)=>{ console.log("Route middleware"); next(); }, (req,res)=>{ res.send("User Page"); });

3. Built-in Middleware

Express provides built-in middleware like:

  • express.json() – parse JSON data
  • express.urlencoded() – parse form data
  • express.static() – serve static files
javascript app.use(express.json());

4. Third-Party Middleware

Middleware created by external libraries, such as:

  • morgan (logging)
  • cors (cross-origin requests)
  • helmet (security)
javascript const morgan = require("morgan"); app.use(morgan("dev"));

5. Error-Handling Middleware

Special middleware used to handle errors. It has four parameters:

javascript app.use((err, req, res, next) => { res.status(500).send("Something went wrong"); });

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.

Quick Summary: Middleware functions run between request and response, allowing you to process, modify, or control application flow in Express.js.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators