http2stream.sendTrailers(headers)


向连接的 HTTP/2 对等端发送尾随的 HEADERS 帧。 此方法将导致 Http2Stream 立即关闭,并且只能在 'wantTrailers' 事件触发后调用。 当发送请求或发送响应时,必须设置 options.waitForTrailers 选项,以便在最后的 DATA 帧之后保持 Http2Stream 打开,以便可以发送尾随标头。

const http2 = require('node:http2');
const server = http2.createServer();
server.on('stream', (stream) => {
  stream.respond(undefined, { waitForTrailers: true });
  stream.on('wantTrailers', () => {
    stream.sendTrailers({ xyz: 'abc' });
  });
  stream.end('Hello World');
});

HTTP/1 规范禁止尾随标头包含 HTTP/2 伪标头字段(例如 ':method'':path' 等)。

Sends a trailing HEADERS frame to the connected HTTP/2 peer. This method will cause the Http2Stream to be immediately closed and must only be called after the 'wantTrailers' event has been emitted. When sending a request or sending a response, the options.waitForTrailers option must be set in order to keep the Http2Stream open after the final DATA frame so that trailers can be sent.

const http2 = require('node:http2');
const server = http2.createServer();
server.on('stream', (stream) => {
  stream.respond(undefined, { waitForTrailers: true });
  stream.on('wantTrailers', () => {
    stream.sendTrailers({ xyz: 'abc' });
  });
  stream.end('Hello World');
});

The HTTP/1 specification forbids trailers from containing HTTP/2 pseudo-header fields (e.g. ':method', ':path', etc).