Middleware Basics Interview Questions

4 questions developers actually get asked about middleware basics — with clear, practical answers you can use to prepare.

Q1. What is middleware in web development?

Middleware is code that runs before (or after) your main route handler, on every request that matches its scope. It is used for cross-cutting concerns like authentication, logging, and rate limiting so the same logic does not get copy-pasted into every route.

Q2. How is middleware different from a decorator?

Middleware runs automatically on all requests in its scope (global, per-route-group, etc.) without the route author opting in. A decorator is a function wrapper applied explicitly to one function at a time. Middleware is opt-out; decorators are opt-in.

Q3. When should you NOT use middleware?

When the logic only applies to one route. Putting a single-route check into global middleware forces every other request to evaluate it, which adds latency and complicates debugging. Keep one-off checks inside the route itself.

Q4. What does it mean for middleware to "pass" a request?

After running its check or transformation, middleware either calls the next handler in the chain (passing the request along) or short-circuits with a response like a redirect or 401 error. Forgetting to do either causes the request to hang.

Want the full concept, analogies, and AI prompts for middleware basics?

Read the full Middleware Basics block →