Skip to content
vv1.14.0
Main

HTTP/2

This boilerplate comes with native, high-performance support for HTTP/2, enabling multiplexing, header compression, and server push capabilities out of the box.

Why?

HTTP/2 is a major revision of the HTTP network protocol. It improves latency and throughput by moving away from the text-based, ordered nature of HTTP/1.1.

Key Benefits

FeatureHTTP/1.1HTTP/2Benefit
MultiplexingSequential Requests (Head-of-Line Blocking)Parallel Streams over single TCP connectionRemoves the need for domain sharding and multiple connections.
Header CompressionPlain Text (Redundant)HPACK CompressionDrastically reduces overhead, especially for microservices.
Binary ProtocolText-basedBinary-basedMore efficient to parse and less error-prone.

Visual Comparison

HTTP/1.1HTTP/2

Configuration

HTTP/2 is controlled via environment variables. Since modern browsers enforce HTTPS for HTTP/2, this boilerplate automatically configures TLS/SSL when enabled.

1. Enable in Environment

Set APP_HTTP2 to true in your .env file. You must also point to your SSL certificate and private key.

dotenv
# .env
APP_HTTP2="true"
APP_KEY="./keys/private.pem"
APP_CERT="./keys/cert.pem"

2. Generate Local Certificates

For local development, we include a utility to generate valid self-signed certificates and RSA keys in one command:

bash
bun gen:keys

This command uses openssl to automatically generate:

  • 🔑 private.pem: Your private key.
  • 📜 cert.pem: A self-signed SSL certificate valid for 365 days.

🧪 How to Test

Since we are using self-signed certificates locally, testing requires bypassing standard trust verification.

Option A: Using cURL

Use the -k (insecure) flag to bypass certificate validation and --http2 to verify the protocol negotiation.

bash
curl -I -k --http2 https://localhost:3000

✅ Expected Output:

http
HTTP/2 200
content-type: application/json
...

Option B: Using Browser

  1. 🌐 Open https://localhost:3000.
  2. ⚠️ You will see a "Your connection is not private" warning (normal for self-signed certs).
  3. Click Advanced -> Proceed to localhost (unsafe).
  4. Open Developer Tools (F12) -> Network tab.
  5. Right-click the header columns and enable Protocol.
  6. Refresh the page. You should see h2 in the Protocol column.

⚠️ Production Note

In production (e.g., behind Nginx, ALB, or Cloudflare), you typically terminate SSL at the load balancer. However, if you need end-to-end encryption or your load balancer supports HTTP/2 upstream, this configuration allows Node.js/Bun to handle it natively.

TIP

If you are using a reverse proxy like Nginx, you might not need APP_HTTP2="true" in the Node app if Nginx handles the HTTP/2 client connection and proxies via HTTP/1.1 to the backend. Enable this only if you need HTTP/2 capabilities directly in the application server.