Middleware
PronunciationMID-ul-wair
Definition
Code that runs between receiving a request and sending the response, used for things like logging, authentication, and error handling.
Where you hear it
Express, NestJS, Django, and Next.js backend discussions.
Examples
Add a middleware that logs every request.
The auth middleware rejects requests without a valid token.
The rate-limit middleware returns 429 before the request reaches the handler.
Common mistake
In Express, forgetting to call next(). The request then hangs forever.
Don't confuse with
Middleware is often confused with interceptors; while both handle requests, middleware is typically part of the framework's core request pipeline, whereas interceptors are often used to modify data or handle responses at a more granular or service-specific level.
Say it at work
- In a meeting or chat
We should add a new middleware to handle rate limiting for all incoming API calls.
- In an email or ticket
I have implemented a custom middleware to validate the request headers before processing the main logic.