类:IncomingMessage
类:IncomingMessage
处理 HTTP/HTTPS 请求的响应。
Process: Main, Utility
This class is not exported from the 'electron' module. 它只能作为 Electron API 中其他方法的返回值。
ClientRequest实现了Readable Stream接口, 因此是一个EventEmitter类型.
实例事件
Event: 'data'
返回:
chunk缓冲区: 响应体数据块。
data事件是将响应数据转换为应用程序代码的常用方法。
Event: 'end'
指示响应正文已结束。 必须在 'data' 事件之前放置。
Event: 'aborted'
正在进行的 HTTP 事务被取消后触发。
Event: 'error'
返回:
error通常保存一个错误字符串,识别失败的根本原因。
当流响应数据事件遇到错误时发出。 例如, 如果服务器在响应仍在流时关闭基础, 则会在响应对象上发出一个 error 事件, 随后将在请求对象上执行 close 事件。
实例属性
IncomingMessage 实例具有以下可读属性:
response.statusCode
一个指示 HTTP 响应状态代码的 Integer。
response.statusMessage
一个 string 表示 HTTP 状态消息。
response.headers
A Record<string, string | string[]> representing the HTTP response headers. headers对象格式如下:
- 所有的头文件名是小写的。
- 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, oruser-agentare discarded. set-cookie永远是一个数组。 重复项被添加到数组内。- 对于重复的
cookie头,对应的值会用 '; ' 连接 - 所有其他 header 的值通过 ',' 连接
response.httpVersion
表示HTTP协议版本号的 string 。 一般是“1”或“1.1”。 此外 httpVersionMajor 和httpVersionMinor 两整数值可读属性,分别返回HTTP的主要和次要版本号
response.httpVersionMajor
表示HTTP协议主要版本号的 整数。
response.httpVersionMinor
表示http协议次要版本号的整数。
response.rawHeaders
一个 string[] 包含接收到的原始 HTTP 响应头。 键和值在同一列表中。 它不是元组的列表。 所以,偶数偏移量是键,奇数偏移量是值。 标头名称不是被小写过的,并且重复项不会合并。
// 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)