HTTP2 and protocol negotiation
Prerequisites: previous curriculum lessons on Node streams, HTTP, buffers and asynchronous errors. Local lab baseline: Node.js 22.16.0, Linux, ESM. Use a disposable directory. Static examples are not ByteMind interactive execution. Each example is independent; save the named files together and run node main.mjs. The author harness imposes a five-second process deadline.
How it works
Create a cleartext lab server with createServer and a matching connect client. This is prior-knowledge HTTP/2 on loopback, not a browser HTTPS example. For TLS use createSecureServer and verify certificates and ALPN (Application-Layer Protocol Negotiation); the selected protocol depends on both peers. HTTP/2 uses pseudo-headers such as :method and :path. A stream can fail independently, while session failure can affect many requests. HTTP/2 still runs over TCP, so multiplexing does not remove all transport-level blocking. HTTP/3 is a different QUIC-based protocol and is outside this tested API scope.
Multiplex two HTTP/2 requests on one local session
import http2 from 'node:http2';import { once } from 'node:events';const server=http2.createServer();server.on('stream',(stream,headers)=>{stream.respond({':status':200});stream.end('item '+headers[':path']);});server.listen(0,'127.0.0.1');await once(server,'listening');const session=http2.connect('http://127.0.0.1:'+server.address().port);session.setTimeout(1500,()=>session.destroy(new Error('timeout')));await once(session,'connect');async function request(path){ const stream=session.request({':path':path});stream.end(); let body=''; for await(const chunk of stream)body+=chunk; return body;}try {const results=await Promise.all([request('/a'),request('/b')]);console.log(results.join(' | '));}finally {session.close();await once(session,'close');await new Promise(r=>server.close(r));}Expected output:
item /a | item /bTwo requests share the same session; Promise.all returns results in input order, not observed completion order.
Read HTTP/2 pseudo-headers in a synthetic request
const headers={':method':'GET',':path':'/catalog',':scheme':'https',':authority':'example.test','accept':'application/json'};console.log(headers[':method'],headers[':path']);console.log(headers[':authority'],headers.accept);Expected output:
GET /catalogexample.test application/jsonThe object illustrates pseudo-header names; it does not negotiate a protocol.
Common mistakes
Do not close a shared session when only one request finishes. Consume response bodies, close the session after all streams settle, and handle both stream and session errors. Cleartext h2 is not a production substitute for TLS.
Practice
Add a third path and assert its body. Explain what you would need to test before claiming TLS/ALPN compatibility.
Further reading
Official documentation: http2. Internal reference candidate: node-http2-and-protocol-negotiation-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTTP2 and protocol negotiation — knowledge check
Apply the lesson to a fresh scenario and explain the resource or protocol boundary.
What can two HTTP/2 streams share?
What does the cleartext local example establish?
Why should a client handle session failure?