Study notes · 11% of the exam

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. 1

    Headers are sent with the first res.write() or at res.end(); after that, setHeader, writeHead and a second res.send() throw ERR_HTTP_HEADERS_SENT.

  2. 2

    Defaults: keepAliveTimeout 5 s, headersTimeout 60 s, requestTimeout 300 s, server.timeout 0. Set keepAliveTimeout above your load balancer's idle timeout to avoid 502s.

  3. 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. 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. 5

    Express runs middleware in registration order. Error handlers need four parameters, Express 5 forwards rejected promises to them, and trust proxy decides what req.ip means.

  6. 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. 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.ok or res.statusCode and leaving the body unread leaks sockets with both fetch and http.get.

  • A 3-argument "error handler" or a custom handler that ignores err.status and res.headersSent turns client errors into crashes.

  • Retrying every ECONNRESET automatically 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.