http2stream.sendTrailers(headers)


  • headers HTTP/2 头对象

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

【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.】

import { createServer } from 'node:http2';
const server = createServer();
server.on('stream', (stream) => {
  stream.respond(undefined, { waitForTrailers: true });
  stream.on('wantTrailers', () => {
    stream.sendTrailers({ xyz: 'abc' });
  });
  stream.end('Hello World');
});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' 等)。

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