HTTP/2

Multiplexed Streams Over a Single Connection

HTTP/2

Node has built-in HTTP/2. Different API than HTTP/1, but most apps let the reverse proxy do it.

3 min read Level 3/5 #nodejs#http#http2
What you'll learn
  • Recognize HTTP/2's main wins
  • Use http2 with compatibility mode
  • Decide between Node-direct and proxy-handled

HTTP/2 multiplexes many “streams” over one TCP connection, with header compression and server push. Modern browsers prefer it for HTTPS sites.

Node’s node:http2

import { createSecureServer } from "node:http2";
import { readFileSync } from "node:fs";

const server = createSecureServer({
  key:  readFileSync("./certs/key.pem"),
  cert: readFileSync("./certs/cert.pem"),
});

server.on("stream", (stream, headers) => {
  stream.respond({ ":status": 200, "content-type": "text/plain" });
  stream.end("hello over h2");
});

server.listen(443);

HTTP/2 uses streams (within one connection) instead of one request per connection.

Compatibility Mode

node:http2 has a “compat” API where streams look like req/res from node:http:

createSecureServer(options, (req, res) => {
  // looks just like a normal http handler
  res.writeHead(200);
  res.end("hello");
}).listen(443);

Most code stays the same — you get HTTP/2’s wire format underneath.

When to Reach For It

Almost never in app code. In production:

  • Cloudflare / your CDN does HTTP/2 (and HTTP/3) automatically
  • Nginx / Caddy do HTTP/2 with a config flag
  • Node speaks plain HTTP/1 to the proxy

The proxy negotiates the version with the client; you don’t care.

When You Do

  • gRPC services on Node (gRPC requires HTTP/2)
  • Edge cases where you skip a reverse proxy
  • Server-push experiments (mostly abandoned — 103 Early Hints superseded it)

For the typical web API: stick with http, let the proxy handle HTTP/2.

WebSockets →