Skip to content

Commit

Permalink
v12.9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
h7lin committed Sep 2, 2019
1 parent 1737b85 commit e6ae561
Show file tree
Hide file tree
Showing 34 changed files with 101 additions and 64 deletions.
2 changes: 2 additions & 0 deletions http/class_http_clientrequest.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ added: v0.1.17
在消费完数据之前,不会触发 `'end'` 事件。
此外,在读取数据之前,它将占用内存,最终可能导致进程内存不足的错误。

`request` 对象不同,如果响应过早关闭,则 `response` 对象不会触发 `'error'` 事件而是触发 `'aborted'` 事件。

Node.js 不检查 Content-Length 和已传输的主体的长度是否相等。

请求继承自[][Stream],且额外实现以下内容:
Expand Down
2 changes: 1 addition & 1 deletion http/event_checkcontinue.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ added: v0.3.0

处理此事件时,如果客户端应继续发送请求主体,则调用 [`response.writeContinue()`],如果客户端不应继续发送请求主体,则生成适当的 HTTP 响应(例如 `400 Bad Request`)。

请注意,在触发和处理此事件时,不会触发 [`'request'`] 事件。
在触发和处理此事件时,不会触发 [`'request'`] 事件。

2 changes: 1 addition & 1 deletion http/event_checkexpectation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ added: v5.5.0
每次收到带有 HTTP `Expect` 请求头的请求时触发,其中值不是 `100-continue`
如果未监听此事件,则服务器将根据需要自动响应 `417 Expectation Failed`

请注意,在触发和处理此事件时,不会触发 [`'request'`] 事件。
在触发和处理此事件时,不会触发 [`'request'`] 事件。

7 changes: 6 additions & 1 deletion http/event_clienterror.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ changes:
description: The `rawPacket` is the current buffer that just parsed. Adding
this buffer to the error object of `'clientError'` event is to
make it possible that developers can log the broken packet.
- version: v12.0.0
pr-url: https://github.com/nodejs/node/pull/25605
description: The default behavior will return a 431 Request Header
Fields Too Large if a HPE_HEADER_OVERFLOW error occurs.
-->
* `exception` {Error}
Expand All @@ -20,7 +24,8 @@ changes:
此事件的监听器负责关闭或销毁底层套接字。
例如,用户可能希望使用自定义 HTTP 响应更优雅地关闭套接字,而不是突然切断连接。
默认行为是尽可能使用 HTTP `400 Bad Request` 响应关闭套接字,否则立即销毁套接字。
默认行为是尝试使用 HTTP `400 Bad Request` 关闭套接字、或者在 [`HPE_HEADER_OVERFLOW`] 错误的情况下尝试关闭 HTTP `431 Request Header Fields Too Large`。
如果套接字不可写,则会被立即销毁。
`socket` 是发生错误的 [`net.Socket`] 对象。
Expand Down
2 changes: 1 addition & 1 deletion http/event_close_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
added: v0.6.7
-->

表明在调用 [`response.end()`] 或能够刷新之前终止了底层连接
表明底层的连接已被终止

1 change: 0 additions & 1 deletion http/event_close_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ added: v0.4.2
-->

表明底层连接已关闭。
`'end'` 事件一样,每个响应只触发一次此事件。

2 changes: 2 additions & 0 deletions http/event_connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ added: v0.1.0
用户也可以显式触发此事件,以将连接注入 HTTP 服务器。
在这种情况下,可以传入任何 [`Duplex`] 流。

如果在此处调用 `socket.setTimeout()`,则当套接字已提供请求时(如果 `server.keepAliveTimeout` 为非零),超时将会被 `server.keepAliveTimeout` 替换。

18 changes: 14 additions & 4 deletions http/event_information.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@
added: v10.0.0
-->

服务器发送 `1xx` 响应时触发(不包括 `101 Upgrade`)。
使用包含具有状态码的对象的回调触发此事件。
* `info` {Object}
* `httpVersion` {string}
* `httpVersionMajor` {integer}
* `httpVersionMinor` {integer}
* `statusCode` {integer}
* `statusMessage` {string}
* `headers` {Object}
* `rawHeaders` {string[]}


服务器发送 1xx 中间响应(不包括 101 Upgrade)时触发。
此事件的监听器将会接收一个对象,该对象包含 HTTP 版本,状态码,状态消息,键值对请求头对象、以及具有原始请求头名称和值的数组。

```js
const http = require('http');
Expand All @@ -18,8 +28,8 @@ const options = {
const req = http.request(options);
req.end();
req.on('information', (res) => {
console.log(`获得主响应之前的信息: ${res.statusCode}`);
req.on('information', (info) => {
console.log(`获得主响应之前的信息: ${info.statusCode}`);
});
```

Expand Down
2 changes: 1 addition & 1 deletion http/event_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ added: v0.1.0
* `response` {http.ServerResponse}

每次有请求时都会触发。
注意,每个连接可能有多个请求(在 HTTP Keep-Alive 连接的情况下)。
每个连接可能有多个请求(在 HTTP Keep-Alive 连接的情况下)。


4 changes: 2 additions & 2 deletions http/event_upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ added: v0.1.94
const http = require('http');
// 创建 HTTP 服务器。
const srv = http.createServer( (req, res) => {
const srv = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('响应内容');
});
Expand All @@ -25,7 +25,7 @@ srv.on('upgrade', (req, socket, head) => {
'Connection: Upgrade\r\n' +
'\r\n');
socket.pipe(socket);
socket.pipe(socket); // 响应回去。
});
// 服务器正在运行。
Expand Down
2 changes: 1 addition & 1 deletion http/http_createserver_options_requestlistener.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- YAML
added: v0.1.13
changes:
- version: v9.6.0
- version: v9.6.0, v8.12.0
pr-url: https://github.com/nodejs/node/pull/15752
description: The `options` argument is supported now.
-->
Expand Down
2 changes: 1 addition & 1 deletion http/http_get_url_options_callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ changes:

由于大多数请求都是没有主体的 GET 请求,因此 Node.js 提供了这个便捷的方法。
这个方法与 [`http.request()`] 的唯一区别是它将方法设置为 GET 并自动调用 `req.end()`
注意,由于 [`http.ClientRequest`] 章节中所述的原因,回调必须注意消费响应数据。
由于 [`http.ClientRequest`] 章节中所述的原因,回调必须注意消费响应数据。

`callback` 调用时只有一个参数,该参数是 [`http.IncomingMessage`] 的实例。

Expand Down
2 changes: 1 addition & 1 deletion http/http_maxheadersize.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- YAML
added: v10.15.0
added: v11.6.0
-->

* {number}
Expand Down
33 changes: 17 additions & 16 deletions http/http_request_url_options_callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,33 @@ changes:

* `url` {string | URL}
* `options` {Object}
* `protocol` {string} 使用的协议。**默认值:** `'http:'`
* `host` {string} 请求发送至的服务器的域名或 IP 地址。**默认值:** `'localhost'`
* `hostname` {string} `host` 的别名。为了支持 [`url.parse()`],如果同时指定 `host` `hostname`,则使用 `hostname`
* `family` {number} 当解析 `host` `hostname` 时使用的 IP 地址族。有效值为 `4` `6`。如果没有指定,则同时使用 IP v4 v6。
* `port` {number} 远程服务器的端口。**默认值:** `80`
* `localAddress` {string} 为网络连接绑定的本地接口。
* `socketPath` {string} Unix 域套接字。如果指定了 `host` `port` 之一(它们指定了 TCP 套接字),则不能使用此选项。
* `method` {string} 一个字符串,指定 HTTP 请求的方法。**默认值:** `'GET'`
* `path` {string} 请求的路径。应包括查询字符串(如果有)。例如 `'/index.html?page=12'`。当请求的路径包含非法的字符时,则抛出异常。目前只有空格被拒绝,但未来可能会有所变化。**默认值:** `'/'`
* `headers` {Object} 包含请求头的对象。
* `auth` {string} 基本的身份验证,即 `'user:password'`,用于计算授权请求头。
* `agent` {http.Agent | boolean} 控制 [`Agent`] 的行为。可能的值有:
* `undefined` (默认): 对此主机和端口使用 [`http.globalAgent`]
* `Agent` 对象: 显式地使用传入的 `Agent`
* `false`: 使用新建的具有默认值的 `Agent`
* `auth` {string} 基本的身份验证,即 `'user:password'`,用于计算授权请求头。
* `createConnection` {Function} `agent` 选项未被使用时,用来为请求生成套接字或流的函数。这可用于避免创建自定义的 `Agent` 类以覆盖默认的 `createConnection` 函数。详见 [`agent.createConnection()`]。任何[双工流][`Duplex`]都是有效的返回值。
* `timeout` {number}: 指定套接字超时的数值,以毫秒为单位。这会在套接字被连接之前设置超时。
* `defaultPort` {number} 协议的默认端口。 如果使用 `Agent`,则默认值为 `agent.defaultPort`,否则为 `undefined`
* `family` {number} 当解析 `host` `hostname` 时使用的 IP 地址族。有效值为 `4` `6`。如果没有指定,则同时使用 IP v4 v6。
* `headers` {Object} 包含请求头的对象。
* `host` {string} 请求发送至的服务器的域名或 IP 地址。**默认值:** `'localhost'`
* `hostname` {string} `host` 的别名。为了支持 [`url.parse()`],如果同时指定 `host` `hostname`,则使用 `hostname`
* `localAddress` {string} 为网络连接绑定的本地接口。
* `method` {string} 一个字符串,指定 HTTP 请求的方法。**默认值:** `'GET'`
* `path` {string} 请求的路径。应包括查询字符串(如果有)。例如 `'/index.html?page=12'`。当请求的路径包含非法的字符时,则抛出异常。目前只有空格被拒绝,但未来可能会有所变化。**默认值:** `'/'`
* `port` {number} 远程服务器的端口。**默认值:** `defaultPort`(如果有设置)或 `80`
* `protocol` {string} 使用的协议。**默认值:** `'http:'`
* `setHost` {boolean}: 指定是否自动添加 `Host` 请求头。**默认值:** `true`
* `socketPath` {string} Unix 域套接字。如果指定了 `host` `port` 之一(它们指定了 TCP 套接字),则不能使用此选项。
* `timeout` {number}: 指定套接字超时的数值,以毫秒为单位。这会在套接字被连接之前设置超时。
* `callback` {Function}
* 返回: {http.ClientRequest}

Node.js 为每个服务器维护多个连接以发出 HTTP 请求。
此函数允许显式地发出请求。

`url` 可以是字符串或 [`URL`] 对象。
如果 `url` 是一个字符串,则会自动使用 [`url.parse()`] 解析它。
如果 `url` 是一个字符串,则会自动使用 [`url.URL()`] 解析它。
如果它是一个 [`URL`] 对象,则会自动转换为普通的 `options` 对象。

如果同时指定了 `url` `options`,则对象会被合并,其中 `options` 属性优先。
Expand Down Expand Up @@ -85,7 +86,7 @@ req.write(postData);
req.end();
```

注意,在示例中调用了 `req.end()`
在示例中调用了 `req.end()`
使用 `http.request()` 时,必须始终调用 `req.end()` 来表示请求的结束,即使没有数据被写入请求主体。

如果在请求期间遇到任何错误(DNS 解析错误、TCP 层的错误、或实际的 HTTP 解析错误),则会在返回的请求对象上触发 `'error'` 事件。
Expand All @@ -97,7 +98,7 @@ req.end();

* 发送 `'Content-Length'` 请求头会禁用默认的分块编码。

* 发送 `'Expect'` 请求头会立即发送请求头。通常情况下,当发送 `'Expect: 100-continue'` 时,应设置超时时间和 `'continue'` 事件的监听器。详见 RFC2616 的第 8.2.3 节。
* 发送 `'Expect'` 请求头会立即发送请求头。通常情况下,当发送 `'Expect: 100-continue'` 时,应设置超时时间和 `'continue'` 事件的监听器。详见 RFC 2616 的第 8.2.3 节。

* 发送授权请求头会使用 `auth` 选项覆盖以计算基本的身份验证。

Expand Down Expand Up @@ -145,4 +146,4 @@ const req = http.request(options, (res) => {
* `res` 对象上的 `'end'` 事件
* `res` 对象上的 `'close'` 事件

注意,设置 `timeout` 选项或使用 `setTimeout()` 函数不会中止请求或执行除添加 `'timeout'` 事件之外的任何操作。
设置 `timeout` 选项或使用 `setTimeout()` 函数不会中止请求或执行除添加 `'timeout'` 事件之外的任何操作。
2 changes: 1 addition & 1 deletion http/message_rawheaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ added: v0.11.6

原始请求头/响应头的列表,与接收到的完全一致。

请注意,键和值位于同一列表中。
键和值位于同一列表中。
它不是元组列表。
因此,偶数偏移是键值,奇数偏移是关联的值。

Expand Down
5 changes: 4 additions & 1 deletion http/new_agent_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ added: v0.3.4
-->

* `options` {Object} 要在代理上设置的可配置选项集。可以包含以下字段:
* `keepAlive` {boolean} 即使没有未完成的请求,也要保持套接字,这样它们就可以被用于将来的请求而无需重新建立 TCP 连接。**默认值:** `false`
* `keepAlive` {boolean} 即使没有未完成的请求,也要保持套接字,这样它们就可以被用于将来的请求而无需重新建立 TCP 连接。
不要与 `Connection` 请求头的 `keep-alive` 值混淆。
`Connection: keep-alive` 请求头始终在使用代理时发送,除非明确指定 `Connection` 请求头、或者 `keepAlive` `maxSockets` 选项分别设置为 `false` `Infinity`,在这种情况下将会使用 `Connection: close`
**默认值:** `false`
* `keepAliveMsecs` {number} 当使用 `keepAlive` 选项时,指定用于 TCP Keep-Alive 数据包的[初始延迟][initial delay]。当 `keepAlive` 选项为 `false` `undefined` 时则忽略。**默认值:** `1000`
* `maxSockets` {number} 每个主机允许的套接字的最大数量。**默认值:** `Infinity`
* `maxFreeSockets` {number} 在空闲状态下保持打开的套接字的最大数量。仅当 `keepAlive` 被设置为 `true` 时才相关。**默认值:** `256`
Expand Down
8 changes: 7 additions & 1 deletion http/request_aborted.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<!-- YAML
added: v0.11.14
changes:
- version: v11.0.0
pr-url: https://github.com/nodejs/node/pull/20230
description: The `aborted` property is no longer a timestamp number.
-->

如果请求已中止,则此值是请求中止的时间,自 1970 1 1 00:00:00 UTC 以来的毫秒数。
* {boolean}

如果请求已中止,则 `request.aborted` 属性将会为 `true`

8 changes: 4 additions & 4 deletions http/request_getheader_name.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ added: v1.6.0
* 返回: {any}

读取请求中的一个请求头。
注意,该名称不区分大小写。
该名称不区分大小写。
返回值的类型取决于提供给 [`request.setHeader()`] 的参数。

```js
request.setHeader('content-type', 'text/html');
request.setHeader('Content-Length', Buffer.byteLength(body));
request.setHeader('Cookie', ['type=ninja', 'language=javascript']);
const contentType = request.getHeader('Content-Type');
// contentType 是 'text/html'。
// 'contentType' 是 'text/html'。
const contentLength = request.getHeader('Content-Length');
// contentLength 的类型为数值。
// 'contentLength' 的类型为数值。
const cookie = request.getHeader('Cookie');
// cookie 的类型为字符串数组。
// 'cookie' 的类型为字符串数组。
```

2 changes: 1 addition & 1 deletion http/request_path.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
added: v0.4.0
-->

* {string} The request path.
* {string} 请求的路径。

5 changes: 2 additions & 3 deletions http/request_writableended.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ added: v12.9.0

* {boolean}

Is `true` after [`request.end()`][] has been called. This property
does not indicate whether the data has been flushed, for this use
[`request.writableFinished`][] instead.
在调用 [`request.end()`] 之后为 `true`
此属性不表明是否已刷新数据,对于这种应该使用 [`request.writableFinished`]

3 changes: 1 addition & 2 deletions http/request_writablefinished.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ added: v12.7.0

* {boolean}

Is `true` if all data has been flushed to the underlying system, immediately
before the [`'finish'`][] event is emitted.
如果在触发 [`'finish'`] 事件之前,所有数据都已刷新到底层系统,则为 `true`

2 changes: 1 addition & 1 deletion http/response_addtrailers_headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ added: v0.3.0

只有在使用分块编码进行响应时才会发出尾部响应头; 如果不是(例如,如果请求是 HTTP/1.0),它们将被静默丢弃。

请注意,HTTP 需要发送 `Trailer` 响应头才能发出尾部响应头,并在其值中包含响应头字段列表。
HTTP 需要发送 `Trailer` 响应头才能发出尾部响应头,并在其值中包含响应头字段列表。
例如:

```js
Expand Down
4 changes: 1 addition & 3 deletions http/response_finished.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ added: v0.0.2

* {boolean}

布尔值,表明响应是否已完成。
默认为 `false`
[`response.end()`] 执行之后,该值将为 `true`
如果调用了 [`response.end()`],则 `response.finished` 属性将会是 `true`

3 changes: 2 additions & 1 deletion http/response_flushheaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
added: v1.6.0
-->

Flushes the response headers. See also: [`request.flushHeaders()`][].
刷新响应头。
另可参阅:[`request.flushHeaders()`]

2 changes: 1 addition & 1 deletion http/response_getheader_name.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ added: v0.4.0
* 返回: {any}

读出已排队但未发送到客户端的响应头。
请注意,该名称不区分大小写。
该名称不区分大小写。
返回值的类型取决于提供给 [`response.setHeader()`] 的参数。

```js
Expand Down
2 changes: 1 addition & 1 deletion http/response_hasheader_name.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ added: v7.7.0
* 返回: {boolean}

如果当前在传出的响应头中设置了由 `name` 标识的响应头,则返回 `true`
请注意,响应头名称匹配不区分大小写。
响应头名称匹配不区分大小写。

```js
const hasContentType = response.hasHeader('content-type');
Expand Down
2 changes: 1 addition & 1 deletion http/response_statuscode.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
added: v0.4.0
-->

* {number}
* {number} **默认值:** `200`

当使用隐式的响应头时(没有显式地调用 [`response.writeHead()`]),此属性控制在刷新响应头时将发送到客户端的状态码。

Expand Down
5 changes: 2 additions & 3 deletions http/response_writableended.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ added: v12.9.0

* {boolean}

Is `true` after [`response.end()`][] has been called. This property
does not indicate whether the data has been flushed, for this use
[`response.writableFinished`][] instead.
在调用 [`response.end()`] 之后为 `true`
此属性不表明数据是否已刷新,对于这种应该使用 `response.writableFinished`

3 changes: 1 addition & 2 deletions http/response_writablefinished.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ added: v12.7.0

* {boolean}

Is `true` if all data has been flushed to the underlying system, immediately
before the [`'finish'`][] event is emitted.
如果在触发 [`'finish'`] 事件之前,所有数据都已刷新到底层的系统,则为 `true`

2 changes: 1 addition & 1 deletion http/response_write_chunk_encoding_callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ added: v0.1.29
这会发送一块响应主体。
可以多次调用该方法以提供连续的响应主体片段。

注意, `http` 模块中,当请求是 HEAD 请求时,则省略响应主体。
`http` 模块中,当请求是 HEAD 请求时,则省略响应主体。
同样地,`204` `304` 响应不得包含消息主体。

`chunk` 可以是字符串或 buffer。
Expand Down
Loading

0 comments on commit e6ae561

Please sign in to comment.