Skip to main content

Class: IncomingMessage

Class: IncomingMessage

Handle responses to HTTP/HTTPS requests.处理对HTTP/HTTPS请求的响应。

Process:进程:Main
This class is not exported from the 'electron' module. 此类不是从'electron'模块导出的。It is only available as a return value of other methods in the Electron API.它只能作为Electron API中其他方法的返回值使用。

IncomingMessage implements the Readable Stream interface and is therefore an EventEmitter.IncomingMessage实现了Readable Stream接口,因此是一个EventEmitter

Instance Events实例事件

Event: 'data'

Returns:返回:

  • chunk Buffer - A chunk of response body's data.响应主体的数据块。

The data event is the usual method of transferring response data into applicative code.data事件是将响应数据转换为应用代码的常用方法。

Event: 'end'

Indicates that response body has ended. Must be placed before 'data' event.指示响应正文已结束。必须放置在“数据”事件之前。

Event: 'aborted'

Emitted when a request has been canceled during an ongoing HTTP transaction.在正在进行的HTTP事务中取消请求时发出。

Event: 'error'

Returns:返回:

error Error - Typically holds an error string identifying failure root cause.通常保存一个错误字符串,用于识别故障的根本原因。

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.例如,如果服务器在响应仍在流式传输时关闭了底层,则会在响应对象上发出error事件,随后会在请求对象上发出关闭事件。

Instance Properties实例属性

An IncomingMessage instance has the following readable properties:IncomingMessage实例具有以下可读的属性:

response.statusCode

An Integer indicating the HTTP response status code.一个Integer,指示HTTP响应状态代码。

response.statusMessage

A string representing the HTTP status message.表示HTTP状态消息的string

response.headers

A Record<string, string | string[]> representing the HTTP response headers. 表示HTTP响应标头的Record<string, string | string[]>The headers object is formatted as follows:headers对象的格式如下:

  • All header names are lowercased.所有标头名称都是小写的。
  • 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.重复的ageauthorizationcontent-lengthcontent-typeetagexpiresfromhostif-modified-sinceif-unmodified-sincelast-modifiedlocationmax-forwardsproxy-authorizationrefererretry-afterserveruser-agent会被丢弃。
  • set-cookie is always an array. 始终是一个数组。Duplicates are added to the array.重复项将添加到阵列中。
  • For duplicate cookie headers, the values are joined together with '; '.对于重复的cookie头,这些值用'; '连接在一起。
  • For all other headers, the values are joined together with ', '.对于所有其他标头,这些值用', '连接在一起。

response.httpVersion

A string indicating the HTTP protocol version number. 指示HTTP协议版本号的stringTypical values are '1.0' or '1.1'. 典型值为“1.0”或“1.1”。Additionally httpVersionMajor and httpVersionMinor are two Integer-valued readable properties that return respectively the HTTP major and minor version numbers.此外,httpVersionMajorhttpVersionCMinor是两个整型可读属性,分别返回HTTP主版本号和次版本号。

response.httpVersionMajor

An Integer indicating the HTTP protocol major version number.一个Integer,表示HTTP协议的主要版本号。

response.httpVersionMinor

An Integer indicating the HTTP protocol minor version number.一个Integer,表示HTTP协议次要版本号。

response.rawHeaders

A string[] containing the raw HTTP response headers exactly as they were received. 一个string[],其中包含与接收到的原始HTTP响应标头完全相同的原始响应标头。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(request.rawHeaders)