HTTP/2
http2.createServer()
Creates a cleartext HTTP/2 server. Public endpoints typically require TLS and negotiated protocol support. This example uses cleartext HTTP/2 on loopback only, not public TLS negotiation. Tested on Node.js 22.16.0. Example output (one line per entry): ["catalog"].
Formal Syntax & Structure
nodejs syntaxnodejs
createServer([options][, onRequestHandler])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| options | object | Server configuration. |
| onRequestHandler | Function | Optional compatibility request listener. |
Return Value
Http2Server
Server instance.
Working Example
Example Snippetnodejs
import { createServer, connect, constants } from 'node:http2';import { once } from 'node:events';import { text } from 'node:stream/consumers';const server = createServer();server.on('stream', (stream, headers) => { stream.respond({ ':status': 200 }); stream.end('catalog'); });server.listen(0, '127.0.0.1');await once(server, 'listening');const client = connect('http://127.0.0.1:' + server.address().port);try { await once(client, 'connect'); const r = client.request({ ':path': '/' }); r.end(); console.log(await text(r));}finally { const closed = once(client, 'close'); client.destroy(); await closed; await new Promise(resolve => server.close(resolve));}