This project demonstrates the performance differences between HTTP/1.1 and HTTP/2 protocols in Node.js.
.
├── http1.js # HTTP/1.1 server implementation
├── http2.js # HTTP/2 server implementation
├── cert.pem # SSL certificate for HTTP/2
├── key.pem # SSL private key for HTTP/2
└── README.md # This file
HTTP/1.1 is the traditional protocol that:
HTTP/2 is the modern protocol that:
# Generate private key
openssl genrsa -out key.pem 2048
# Generate certificate
openssl req -new -x509 -key key.pem -out cert.pem -days 365
node http1.js
Server starts on port 3000.
node http2.js
Server starts on port 3443 (HTTPS).
GET / - Fast responseGET /slow - Slow response (5 second delay)GET / - Fast responseGET /slow - Slow response (5 second delay)HTTP/1.1:
http://localhost:3000/
http://localhost:3000/slow
HTTP/2:
https://localhost:3443/
https://localhost:3443/slow
Note: For HTTP/2, your browser will show a security warning since we’re using a self-signed certificate. Click “Advanced” and “Proceed to localhost (unsafe)” to continue.
You can use tools like Apache Bench or curl to test performance:
# Test HTTP/1.1
curl http://localhost:3000/
# Test HTTP/2
curl -k https://localhost:3443/
Both servers implement the same logic:
The key difference is in how the protocols handle multiple concurrent requests:
const http = require("http");
http.createServer((req, res) => {
if (req.url === "/slow") {
setTimeout(() => {
res.end("SLOW RESPONSE");
}, 5000);
} else {
res.end("FAST RESPONSE");
}
}).listen(3000, () => {
console.log("HTTP/1.1 server on port 3000");
});
const http2 = require("http2");
const fs = require("fs");
const server = http2.createSecureServer({
key: fs.readFileSync("key.pem"),
cert: fs.readFileSync("cert.pem"),
});
server.on("stream", (stream, headers) => {
const path = headers[":path"];
if (path === "/slow") {
setTimeout(() => {
stream.end("SLOW H2");
}, 5000);
} else {
stream.end("FAST H2");
}
});
server.listen(3443, () => {
console.log("HTTP/2 server on 3443");
});
| Feature | HTTP/1.1 | HTTP/2 |
|---|---|---|
| Protocol | Text-based | Binary |
| Connections | Multiple | Single |
| Multiplexing | No | Yes |
| Header Compression | No | Yes |
| Server Push | No | Yes |
| Security | Optional (HTTPS) | Required (TLS) |
Stop the servers with Ctrl+C in their respective terminals.