Server Middleware
ELI5 — The Vibe Check
Server middleware is code that runs between receiving a request and sending a response. It's like TSA checkpoints at the airport — every request goes through them in order. Logging, authentication, CORS, compression — all middleware. They can modify the request, reject it, or pass it along.
Real Talk
Server middleware are functions that intercept and process HTTP requests and responses in a pipeline. Each middleware can inspect/modify the request, execute logic, and either pass control to the next middleware or short-circuit the chain by sending a response. They enable cross-cutting concerns like authentication, logging, and error handling.
Show Me The Code
// Express middleware
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
req.startTime = Date.now();
next();
});
app.use(cors());
app.use(express.json());
When You'll Hear This
"Add auth middleware before the protected routes." / "The CORS middleware needs to run before everything else."
Related Terms
Authentication (AuthN)
Authentication is proving you are who you say you are.
CORS (CORS)
CORS (Cross-Origin Resource Sharing) is the browser's built-in protection that prevents random websites from making API calls to your backend using the vis...
Middleware Chain
A middleware chain is a series of functions that requests pass through, one after another, like an assembly line.
Request Lifecycle
The request lifecycle is the journey your HTTP request takes from the moment it hits the server to when a response goes back.