Web Framework Internals interview question · Most asked
How does Express handle a request, and how are errors routed?
Summary
Middleware and route handlers run in registration order; each responds or calls next(). next(err) or a synchronous throw skips to error-handling middleware (functions with four parameters). Unmatched requests get a 404. Express 4 ignores rejected promises from async handlers; Express 5 forwards them to error handlers.
Longer Answer
Register error handlers last, with four parameters, and never send a response twice.
Key Points
- Registration order; next() continues.
- Four-parameter error handlers.
- Express 5 handles async rejections.
- 404 when nothing responds.
Deep dive: Request Lifecycle: Express: The Middleware Stack and Error Handling
Explanation
What interviewers look for
Arity rule and async error behaviour.
More most-asked Web Framework Internals questions
- Where should authentication, authorisation, validation and error formatting live in a web framework?
- What is the NestJS request lifecycle?
- Describe the Spring MVC request flow and where exceptions are handled.
- What is a request lifecycle, and why should a backend engineer know it for their framework?
- Walk through the Laravel request lifecycle and say where you would add code that must run on every request.
- What layers does a request pass through before reaching application code, and how should their timeouts relate?
- Walk through the Django request lifecycle.
- What happens in the browser from navigation to an interactive single-page app?