Skip to main content

类:IncomingMessage

类:IncomingMessage

🌐 Class: IncomingMessage

处理 HTTP/HTTPS 请求的响应。

进程:主进程工具
此类未从 'electron' 模块导出。它仅作为 Electron API 中其他方法的返回值可用。

IncomingMessage 实现了 Readable Stream 接口,因此它是一个 事件触发器

实例事件

🌐 Instance Events

Event: 'data'

返回:

🌐 Returns:

  • chunk 缓冲区 - 响应主体数据的一部分。

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

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

Event: 'end'

表示响应主体已结束。必须放在 'data' 事件之前。

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

Event: 'aborted'

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

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

Event: 'error'

返回:

🌐 Returns:

  • error 错误 - 通常包含一个错误字符串,用于识别失败的根本原因。

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

🌐 Emitted when an error was encountered while streaming response data events. For instance, if the server closes the underlying connection 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

一个表示 HTTP 状态消息的 string

🌐 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:

  • 所有标头名称均为小写。
  • 重复的 ageauthorizationcontent-lengthcontent-typeetagexpiresfromhostif-modified-sinceif-unmodified-sincelast-modifiedlocationmax-forwardsproxy-authorizationrefererretry-afterserveruser-agent 会被丢弃。
  • set-cookie 总是一个数组。重复项会被添加到数组中。
  • 对于重复的 cookie 头,值会用 '; ' 连接在一起。
  • 对于所有其他标题,值将使用“, ”连接在一起。

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)