intermediate

Request lifecycle

Trace a request through parsing, auth, validation, business logic, persistence, response, logging, and cleanup.

An HTTP request in Node traverses layers from socket to response — understanding the order prevents duplicated work and missing cleanup.

Typical flow:

  1. TCP connection accepted; TLS termination if applicable.
  2. HTTP parser builds `req`/`res` (or framework equivalents).
  3. Middleware: correlation ID, body parser, auth, rate limit.
  4. Route handler: validation, business logic, persistence.
  5. Response serialization and headers (status, cache, security).
  6. Logging/metrics with duration and outcome.
  7. Connection reuse (keep-alive) or close; cleanup listeners.
					Client → load balancer → Node server → middleware chain → handler → DB/cache
                ↑___________________________________|
                     response + logging
				

Long-running handlers block concurrency on the same instance if they hold the event loop or connection without streaming/timeouts.

On interviews: walk one POST request end-to-end; say where auth vs validation vs business rules belong; mention timeout and abort (AbortSignal).

Common pitfalls: parsing body before auth; logging PII; no request timeout at edge or server.

The trade-off is balancing simplicity, performance, safety, and operability — name which axis you optimized and what cost you accepted.

Checklist:

  • Name ordered cross-cutting steps.
  • Propagate request/correlation ID through async work.
  • Set server and upstream timeouts.
  • Clean up resources on `res.close` / `aborted`.