What is Middleware in Express and How It Works

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
Search for a command to run...

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
No comments yet. Be the first to comment.
Every day, millions of users upload photos, videos, stories, and reels to Instagram. From a user's perspective, the process appears simple: select media, apply filters, add a caption, and tap "Post."
Building Offline-First Messaging Apps: How Messages Work Without Internet Modern messaging applications have transformed the way people communicate. Whether it's chatting with friends, collaborating w
In this article we'll explore about the Expo Router and React Navigation and answer which one to use in 2026. If you build mobile apps using React Native, one thing becomes obvious very quickly: Navig

Modern mobile apps are no longer just a collection of screens connected together. Apps like Instagram, WhatsApp, Uber, and Netflix operate at massive scale with millions of users, real time systems, o
In this article we'll be exploring react.js and the things of react.js that makes it popular and stand out among other libraries ( no fight over library vs framework ). We'll go through: What problem

Shkaai
68 posts
When building applications with Express, one concept you will see everywhere is middleware. It is one of the most important parts of how Express works.
If you understand middleware properly, you can control how requests are processed, validated, and handled in your application.
Middleware is simply a function that runs between the request and the response.
It has access to:
req (request)
res (response)
next (a function to move to the next step)
function middleware(req, res, next) {
// do something
next();
}
Middleware can:
Modify request or response
Execute some logic
End the request-response cycle
Pass control to the next middleware
When a client sends a request, it does not go directly to the route handler.
Instead, it passes through a chain of middleware functions.
Request → Middleware → Middleware (if there in another one) → Route Handler → Response
Each middleware gets a chance to:
Process the request
Decide whether to continue or stop
Express provides different types of middleware based on how and where they are used.
These are applied to the entire app.
const express = require('express');
const app = express();
app.use((req, res, next) => {
console.log('App-level middleware');
next();
});
This runs for every request.
These are applied to specific routes using a router.
const router = express.Router();
router.use((req, res, next) => {
console.log('Router-level middleware');
next();
});
router.get('/users', (req, res) => {
res.send('Users route');
});
app.use('/api', router);
This runs only for routes under /api.
Express provides some built-in middleware.
Examples:
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
These help in:
Parsing JSON
Handling form data
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();
});
app.get('/', (req, res) => {
res.send('Done');
});
First
Second
If the order changes, the behaviour also changes.
The next() function is used to pass control to the next middleware.
If you do not call next():
The request will stop
The client will not get a response (request hangs)
app.use((req, res, next) => {
console.log('Middleware running');
next();
});
You can also stop the flow:
app.use((req, res) => {
res.send('Request ended here');
});
Middleware is used in almost every backend application.
app.use((req, res, next) => {
console.log(`\({req.method} \){req.url}`);
next();
});
Logs every incoming request.
function auth(req, res, next) {
const isLoggedIn = true;
if (!isLoggedIn) {
return res.status(401).send('Unauthorized');
}
next();
}
app.get('/profile', auth, (req, res) => {
res.send('Protected profile');
});
Only allows access if the user is authenticated.
app.post('/user', (req, res, next) => {
if (!req.body.name) {
return res.status(400).send('Name is required');
}
next();
}, (req, res) => {
res.send('User created');
});
Ensures correct data before processing.
Middleware is a function that runs between request and response
It sits in the request lifecycle before the final handler
There are different types: application-level, router-level, and built-in
Middleware executes in order
next() controls the flow
It is used for logging, authentication, validation, and more
Middleware is the backbone of Express applications. It allows you to break down request handling into small, reusable steps that process data, enforce rules, and control application flow. By chaining middleware functions together, you can build scalable and maintainable systems. Whether it is logging requests, checking authentication, or validating input, middleware gives you full control over how your application behaves at every stage of a request.