事件:'information'


【Event: 'information'

当服务器发送 1xx 中间响应(不包括 101 Upgrade)时触发。此事件的监听器将接收到一个对象,该对象包含 HTTP 版本、状态码、状态信息、键值对形式的头部对象,以及一个数组,数组中依次包含原始头部名称及其对应的值。

【Emitted when the server sends a 1xx intermediate response (excluding 101 Upgrade). The listeners of this event will receive an object containing the HTTP version, status code, status message, key-value headers object, and array with the raw header names followed by their respective values.】

import { request } from 'node:http';

const options = {
  host: '127.0.0.1',
  port: 8080,
  path: '/length_request',
};

// Make a request
const req = request(options);
req.end();

req.on('information', (info) => {
  console.log(`Got information prior to main response: ${info.statusCode}`);
});const http = require('node:http');

const options = {
  host: '127.0.0.1',
  port: 8080,
  path: '/length_request',
};

// Make a request
const req = http.request(options);
req.end();

req.on('information', (info) => {
  console.log(`Got information prior to main response: ${info.statusCode}`);
});

由于 101 升级状态打破了传统的 HTTP 请求/响应链,例如 WebSocket、就地 TLS 升级或 HTTP 2.0,因此不会触发此事件。要接收 101 升级通知,请改为监听 'upgrade' 事件。