What is HTTP/2 and does Node.js support it?
Answer
HTTP/2 is the second major version of the HTTP protocol, providing significant performance improvements over HTTP/1.1: (1) Multiplexing: multiple requests/responses over a single TCP connection simultaneously (HTTP/1.1 allows only one at a time per connection, leading to head-of-line blocking); (2) Header compression (HPACK): HTTP headers are compressed, reducing overhead significantly for repeated headers; (3) Server Push: server can proactively send resources (CSS, JS) before the client requests them; (4) Binary protocol: more efficient parsing than HTTP/1.1's text protocol. Node.js has a built-in http2 module: const server = http2.createSecureServer({ key, cert }, (req, res) => { res.end("Hello HTTP/2"); }); — requires TLS. Express.js does not natively support HTTP/2 (it uses Node's http module). For HTTP/2 with Express, use spdy package or switch to Fastify (supports HTTP/2 natively). Most production deployments use Nginx or a load balancer as an HTTP/2 terminator, forwarding HTTP/1.1 to Node.js internally — simpler than enabling HTTP/2 in Node.js directly.
Previous
How does HTTPS work and how do you set up TLS in Node.js?
Next
What is the difference between Fastify and Express?
More Node.js Questions
View all →- Advanced How does Node.js handle concurrency without multiple threads?
- Advanced What is the Node.js memory model and how does garbage collection work?
- Advanced What are memory leaks in Node.js and how do you detect them?
- Advanced What is the difference between process.exit() and throwing an uncaught exception?
- Advanced What is the N+1 query problem and how do you solve it in Node.js?