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:
- TCP connection accepted; TLS termination if applicable.
- HTTP parser builds `req`/`res` (or framework equivalents).
- Middleware: correlation ID, body parser, auth, rate limit.
- Route handler: validation, business logic, persistence.
- Response serialization and headers (status, cache, security).
- Logging/metrics with duration and outcome.
- 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`.