Can you use the `http2` module to create an unencrypted HTTP/2 server (`h2c`)? If so, how, and what are the practical limitations?

Node.js interview question for Advanced practice.

Answer

Yes, you can create an unencrypted HTTP/2 server using the http2.createServer() method. This creates a server that operates using the h2c protocol. Implementation: javascript const http2 = require('http2'); const server = http2.createServer(); server.on('stream', (stream, headers) = { stream.respond({ ':status': 200 }); stream.end('Hello h2c'); }); server.listen(3000); Practical Limitations: The most significant limitation is lack of browser support. Virtually all modern web browsers (Chrome, Firefox, Safari, Edge) have chosen not to implement h2c. They will only speak HTTP/2 over an encrypted TLS connection (h2). Therefore, an h2c server is not useful for public-facing websites. Its use is primarily limited to controlled environments, such as service-to-service communication within a trusted backend network, where the overhead of TLS might be considered unnecessary.

Explanation

h2c refers to HTTP/2 Cleartext, which is HTTP/2 running over a standard, unencrypted TCP connection.

Related Questions