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
| Feature | HTTP/1.1 | HTTP/2 | Benefit |
|---|---|---|---|
| Multiplexing | Sequential Requests (Head-of-Line Blocking) | Parallel Streams over single TCP connection | Removes the need for domain sharding and multiple connections. |
| Header Compression | Plain Text (Redundant) | HPACK Compression | Drastically reduces overhead, especially for microservices. |
| Binary Protocol | Text-based | Binary-based | More efficient to parse and less error-prone. |
Visual Comparison
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.
# .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:
bun gen:keysThis 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.
curl -I -k --http2 https://localhost:3000✅ Expected Output:
HTTP/2 200
content-type: application/json
...Option B: Using Browser
- 🌐 Open
https://localhost:3000. - ⚠️ You will see a "Your connection is not private" warning (normal for self-signed certs).
- Click Advanced -> Proceed to localhost (unsafe).
- Open Developer Tools (
F12) -> Network tab. - Right-click the header columns and enable Protocol.
- Refresh the page. You should see
h2in 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.