What is Middleware in Express and How It Works

As your backend grows, handling requests is not just about sending responses.
Before a request reaches your route handler, you often need to:
log information
check authentication
validate data
You don’t want to repeat this logic inside every route.
This is where middleware comes in.
What is Middleware
Middleware in Express is a function that runs between the request and the response.
It sits in the middle. It receives the request, processes it, and either passes it forward or ends the response.
You can think of middleware as a checkpoint. Every request passes through these checkpoints before reaching its final destination.
Where Middleware Fits in the Lifecycle
When a request comes to your server, it does not directly go to the route handler.
It goes through a pipeline.
Request → Middleware → Middleware → Route Handler → Response
Each middleware gets a chance to:
modify the request
perform some logic
decide whether to continue
Basic Middleware Example
const express = require("express");
const app = express();
app.use((req, res, next) => {
console.log("Middleware executed");
next();
});
app.get("/", (req, res) => {
res.send("Home Page");
});
app.listen(3000);
Here:
the middleware runs first
then the request moves to the route
The Role of next()
The next() function is what moves the request forward.
If you do not call next(), the request will stop.
app.use((req, res, next) => {
console.log("Checking something");
next();
});
Without next(), the request never reaches the route handler.
This is one of the most important parts of middleware.
Types of Middleware
Express supports different types of middleware depending on how and where you use them.
Application-Level Middleware
These are applied to the entire app.
app.use((req, res, next) => {
console.log("Runs for every request");
next();
});
Router-Level Middleware
These are applied to specific routes.
app.get("/user", (req, res, next) => {
console.log("Route-specific middleware");
next();
}, (req, res) => {
res.send("User route");
});
Built-in Middleware
Express provides built-in middleware for common tasks.
app.use(express.json());
This helps parse JSON data from requests.
Execution Order of Middleware
Middleware runs in the order it is defined.
app.use((req, res, next) => {
console.log("First");
next();
});
app.use((req, res, next) => {
console.log("Second");
next();
});
Output:
First
Second
Order matters.
If middleware is placed incorrectly, your logic may not work as expected.
Real-World Examples
Middleware is used in almost every backend application.
Logging
app.use((req, res, next) => {
console.log(req.method, req.url);
next();
});
Authentication
app.use((req, res, next) => {
const isAuthenticated = true;
if (!isAuthenticated) {
return res.send("Access denied");
}
next();
});
Request Validation
app.use((req, res, next) => {
if (!req.body.name) {
return res.send("Name is required");
}
next();
});
Middleware as a Pipeline
You can visualize middleware like a chain.
Request → Logger → Auth → Validation → Route → Response
Each step processes the request before passing it forward.
Why Middleware Matters
Middleware allows you to:
keep your code clean
reuse logic
separate concerns
Instead of repeating code in every route, you define it once and apply it globally or selectively.
Final Thought
Middleware is what gives Express its flexibility. It turns a simple request-response system into a structured pipeline. Once you understand middleware, you can control how requests flow through your application. That is when your backend starts becoming powerful.






