Lifecycle of an HTTP Request in Express and Fastify

Discover the lifecycle of an HTTP request in Express and Fastify. Learn why forgetting next() causes timeouts and how to debug it. Improve the performance of

miércoles, 8 de julio de 2026 • 3 min read • Q2BSTUDIO Team

Errors that block your Node.js server

Understanding the lifecycle of an HTTP request in frameworks like Express or Fastify is not a mere academic exercise; it is a practical skill that makes the difference between an application that responds smoothly and a system that, without warning, leaves users waiting until the socket timeout releases them. In a context where every millisecond counts and user experience is the main asset, mastering this flow is essential for any team developing custom applications with Node.js.

When an HTTP client sends a request, the Node.js server starts a process that goes far beyond 'calling the handler and returning a response'. In Express, that process is organized as a sequential stack of middleware. Each piece of middleware receives the req and res objects along with the next function. If a function does not invoke next or send an explicit response (for example via res.json), the flow silently stops: no exception is thrown, no error appears in the server logs, and the client remains hanging until the socket expires. In production environments, this failure manifests as an increase in the 99th percentile of latency and an accumulation of connections in the ESTABLISHED state, without traditional logs giving any clue. For companies offering AWS and Azure cloud services, detecting and preventing these scenarios is critical to maintaining committed SLAs.

Fastify, on the other hand, replaces the flat middleware stack with a pipeline of hooks with fixed names and order: onRequest, preParsing, preValidation, preHandler, the route handler, preSerialization, onSend, and finally onResponse. Each hook is an asynchronous function whose progress depends on the promise being resolved or rejected. If a hook forgets to call reply.send() or does not return a value (in the case of the handler), the pipeline freezes exactly as in Express, but with the advantage that the framework itself can detect the blockage if appropriate timeouts are used at the server level. This design difference makes Fastify especially attractive for projects requiring high performance under JSON loads, as is often the case in artificial intelligence and process automation solutions where each request must be validated, serialized, and returned in the shortest possible time.

The most common failure modes go beyond simply forgetting next(). There is also duplicate responses —sending headers twice throws the ERR_HTTP_HEADERS_SENT error— and middleware ordering issues: placing an error handler before routes or an authentication layer after the controller leaves the application exposed to unpredictable behavior. In cybersecurity projects, for example, a misplacement of the authorization middleware can open inadvertent security gaps. The recommended practice is to always register global routes before specific controllers and always use the return statement when sending the response to avoid subsequent code execution.

To debug these issues, monitoring based on distributed traces has become the most effective tool. Instrumenting the application with OpenTelemetry allows you to see, in a single panel, which middleware or hook is consuming time or has been left open without completing. Additionally, setting timebounds at the HTTP server level (requestTimeout, headersTimeout) protects against slow clients and prevents sockets from accumulating indefinitely. At Q2BSTUDIO we systematically apply these techniques when developing custom software for our clients, integrating business intelligence services like Power BI to visualize latency metrics and detect anomalies before they affect end users.

The choice between Express and Fastify largely depends on the application profile. Express offers immense flexibility thanks to its middleware ecosystem and ease of learning, ideal for rapid prototyping and teams that prioritize familiarity. Fastify, with its radix trie-based router and compiled serialization via schemas, offers notably superior performance under high JSON loads, something that is decisive in enterprise AI projects or in building AI agents that process multiple requests in real time. In any case, the key is to understand that each request follows a predictable path and that carelessness at any step can have measurable consequences in production. At Q2BSTUDIO we help our clients design these architectures with the right balance of speed, security, and maintainability, ensuring that every line of code contributes to an exceptional user experience.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.