HTTP/2

serverHttp2Stream.respond()

Sends response headers before the body. Pseudo-headers such as :status belong to the HTTP/2 header map. 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): ["202 text/plain queued"].

Formal Syntax & Structure

nodejs syntaxnodejs
stream.respond([headers[, options]])

Parameters & Attributes

NameTypeDescription
headersobjectResponse headers.
optionsobjectResponse stream options.

Return Value

undefined

Commits response headers.

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': 202, 'content-type': 'text/plain' }); stream.end('queued'); });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': '/' });    const headers = once(r, 'response');    r.end();    const [h] = await headers;    console.log(h[':status'], h['content-type'], await text(r));}finally {    const closed = once(client, 'close');    client.destroy();    await closed;    await new Promise(resolve => server.close(resolve));}
Node.js Reference