Intermediate LevelNode JS
What are the core advantages of HTTP/2 in Node.js (node:http2) over HTTP/1.1?
❓ Interview Prompt: Explain `node:http2` module, binary frame protocol, header compression (HPACK), request multiplexing over a single TCP connection.
HTTP/2 ('node:http2') improves web performance over legacy HTTP/1.1:
Key Advantages:
1
Binary Framing: Encodes messages into binary frames rather than plain text.
2
Multiplexing: Sends multiple concurrent HTTP requests/responses over a single TCP connection, eliminating head-of-line blocking!
3
HPACK Header Compression: Compresses HTTP headers to cut network overhead.
Code Implementation Example
javascript
import http2 from 'node:http2';
import fs from 'node:fs';
const server = http2.createSecureServer({
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
});
server.on('stream', (stream, headers) => {
stream.respond({
':status': 200,
'content-type': 'text/html'
});
stream.end('<h1>HTTP/2 Multiplexed Response</h1>');
});
server.listen(8443);💡Creating an HTTP/2 server using node:http2.
🎯 Interview Key Takeaway
Use HTTP/2 multiplexing to serve multiple requests concurrently over a single TCP connection.
#http2#Multiplexing#HPACK#Networking#Intermediate
Back to all Node JS Questions→