Skip to main content

类:IncomingMessage

类:IncomingMessage

¥Class: IncomingMessage

处理对 HTTP/HTTPS 请求的响应。

¥Handle responses to HTTP/HTTPS requests.

进程:主进程, 实用工具
该类不是从 'electron' 模块导出的。它仅可用作 Electron API 中其他方法的返回值。

¥Process: Main, Utility
This class is not exported from the 'electron' module. It is only available as a return value of other methods in the Electron API.

IncomingMessage 实现了 可读流 接口,因此是 EventEmitter

¥IncomingMessage implements the Readable Stream interface and is therefore an EventEmitter.

实例事件

¥Instance Events

事件:'data'

¥Event: 'data'

返回:

¥Returns:

  • chunk 缓冲 - 响应主体的数据块。

    ¥chunk Buffer - A chunk of response body's data.

data 事件是将响应数据传输到应用代码的常用方法。

¥The data event is the usual method of transferring response data into applicative code.

事件:'end'

¥Event: 'end'

表示响应正文已结束。必须在 'data' 事件之前放置。

¥Indicates that response body has ended. Must be placed before 'data' event.

事件:'aborted'

¥Event: 'aborted'

在正在进行的 HTTP 事务期间取消请求时发出。

¥Emitted when a request has been canceled during an ongoing HTTP transaction.

事件:'error'

¥Event: 'error'

返回:

¥Returns:

error 错误 - 通常包含标识故障根本原因的错误字符串。

¥error Error - Typically holds an error string identifying failure root cause.

当流响应数据事件时遇到错误时发出。例如,如果服务器在响应仍在流式传输时关闭底层,则响应对象上将发出 error 事件,随后请求对象上将发出 close 事件。

¥Emitted when an error was encountered while streaming response data events. For instance, if the server closes the underlying while the response is still streaming, an error event will be emitted on the response object and a close event will subsequently follow on the request object.

实例属性

¥Instance Properties

IncomingMessage 实例具有以下可读属性:

¥An IncomingMessage instance has the following readable properties:

response.statusCode

Integer 表示 HTTP 响应状态代码。

¥An Integer indicating the HTTP response status code.

response.statusMessage

string 代表 HTTP 状态消息。

¥A string representing the HTTP status message.

response.headers

Record<string, string | string[]> 代表 HTTP 响应标头。headers 对象的格式如下:

¥A Record<string, string | string[]> representing the HTTP response headers. The headers object is formatted as follows:

  • 所有标头名称均为小写。

    ¥All header names are lowercased.

  • ageauthorizationcontent-lengthcontent-typeetagexpiresfromhostif-modified-sinceif-unmodified-sincelast-modifiedlocationmax-forwardsproxy-authorizationrefererretry-afterserveruser-agent 的重复项将被丢弃。

    ¥Duplicates of age, authorization, content-length, content-type, etag, expires, from, host, if-modified-since, if-unmodified-since, last-modified, location, max-forwards, proxy-authorization, referer, retry-after, server, or user-agent are discarded.

  • set-cookie 始终是一个数组。重复项将添加到数组中。

    ¥set-cookie is always an array. Duplicates are added to the array.

  • 对于重复的 cookie 标头,这些值用 '; 连接在一起。'。

    ¥For duplicate cookie headers, the values are joined together with '; '.

  • 对于所有其他标头,这些值与 ',' 连接在一起。

    ¥For all other headers, the values are joined together with ', '.

response.httpVersion

string 表示 HTTP 协议版本号。典型值为 '1.0' 或 '1.1'。另外 httpVersionMajorhttpVersionMinor 是两个整数值可读属性,分别返回 HTTP 主要版本号和次要版本号。

¥A string indicating the HTTP protocol version number. Typical values are '1.0' or '1.1'. Additionally httpVersionMajor and httpVersionMinor are two Integer-valued readable properties that return respectively the HTTP major and minor version numbers.

response.httpVersionMajor

Integer 表示 HTTP 协议的主版本号。

¥An Integer indicating the HTTP protocol major version number.

response.httpVersionMinor

Integer 表示 HTTP 协议次版本号。

¥An Integer indicating the HTTP protocol minor version number.

response.rawHeaders

string[] 包含与收到的原始 HTTP 响应标头完全相同的内容。键和值位于同一列表中。它不是元组列表。因此,偶数数字的偏移量是键值,奇数数字的偏移量是关联值。标头名称不是小写的,并且不会合并重复项。

¥A string[] containing the raw HTTP response headers exactly as they were received. The keys and values are in the same list. It is not a list of tuples. So, the even-numbered offsets are key values, and the odd-numbered offsets are the associated values. Header names are not lowercased, and duplicates are not merged.

// Prints something like:
//
// [ 'user-agent',
// 'this is invalid because there can be only one',
// 'User-Agent',
// 'curl/7.22.0',
// 'Host',
// '127.0.0.1:8000',
// 'ACCEPT',
// '*/*' ]
console.log(response.rawHeaders)