Top 4 Middleware Interview Questions
Browse 4 real-world technical and behavioral interview questions about Middleware. Review scenarios, edge cases, and architectural best practices.
Your logs are full of Cannot set headers after they are sent. Walk me through how a request gets into that state.
ERR_HTTP_HEADERS_SENT means an Express route tried to set status or headers after the response had already been sent. The usual cause is two response paths: missing return after res.send, async callbacks racing, or an error handler trying to respond after headers are committed.
A request fails but your Express error-handling middleware never runs. How do you work out why?
An Express error handler is skipped when it is not registered after routes, lacks the four-argument signature, or an async error never reaches next(). Express 4 needs explicit next(err) for rejected promises; Express 5 forwards them automatically.
Walk me through the middleware chain of an Express service. What has to come before what, and why?
Middleware runs in registration order, so ordering is behaviour rather than style: the request id precedes logging, the body parser precedes anything reading the body, authentication precedes authorisation, and the error handler goes last because Express only searches forward from the failure.
You added an authorisation policy to an ASP.NET Core endpoint and it is not being enforced. Where do you look?
The pipeline is an ordered list you assembled, and authorisation cannot decide anything before routing has selected an endpoint to decide about. Registered in the wrong order it either runs against nothing or never runs at all, and neither produces an error - the request simply succeeds.