HTTP, Networking and Frameworks
Know how node:http, fetch, Express and Fastify behave in production: when headers are committed, how keep-alive and the server timeouts interact with load balancers, how to shut down gracefully, and how to stream to long-lived clients.
Key points
- 1
Headers are sent with the first
res.write()or atres.end(); after that,setHeader,writeHeadand a secondres.send()throwERR_HTTP_HEADERS_SENT. - 2
Defaults:
keepAliveTimeout5 s,headersTimeout60 s,requestTimeout300 s,server.timeout0. SetkeepAliveTimeoutabove your load balancer's idle timeout to avoid 502s. - 3
Since Node 19 the global agents keep connections alive, and
server.close()drops idle keep-alive sockets while letting active requests finish;closeAllConnections()is the hard stop. - 4
fetch resolves for every HTTP status, follows redirects, has no overall deadline (use
AbortSignal.timeout), and holds its connection until you read or cancel the body. - 5
Express runs middleware in registration order. Error handlers need four parameters, Express 5 forwards rejected promises to them, and
trust proxydecides whatreq.ipmeans. - 6
TCP is a byte stream, so add framing yourself. WebSockets need heartbeats to find dead peers, and SSE needs
text/event-stream, blank-line-terminated events, heartbeats and no buffering. - 7
In Fastify, response schemas drop undeclared fields, and decorators stay inside their plugin context unless you wrap the plugin with
fastify-plugin.
Common traps
Reading only
res.okorres.statusCodeand leaving the body unread leaks sockets with both fetch andhttp.get.A 3-argument "error handler" or a custom handler that ignores
err.statusandres.headersSentturns client errors into crashes.Retrying every
ECONNRESETautomatically can repeat a POST; retry only idempotent requests or ones protected by an idempotency key.
Test yourself on HTTP, Networking and Frameworks
Ten questions, with the answer and explanation after each one.