Skip to content

Commit abe0148

Browse files
陈刚MylesBorins
陈刚
authored andcommitted
doc: fix http api document
PR-URL: #14625 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]>
1 parent 37c43ed commit abe0148

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

doc/api/http.md

+27-18
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ to keep the Node.js process running when there are no outstanding requests.
7474
It is good practice, to [`destroy()`][] an `Agent` instance when it is no
7575
longer in use, because unused sockets consume OS resources.
7676

77-
Sockets are removed from an agent's pool when the socket emits either
77+
Sockets are removed from an agent when the socket emits either
7878
a `'close'` event or an `'agentRemove'` event. When intending to keep one
79-
HTTP request open for a long time without keeping it in the pool, something
79+
HTTP request open for a long time without keeping it in the agent, something
8080
like the following may be done:
8181

8282
```js
@@ -168,8 +168,9 @@ Called when `socket` is detached from a request and could be persisted by the
168168
Agent. Default behavior is to:
169169

170170
```js
171+
socket.setKeepAlive(true, this.keepAliveMsecs);
171172
socket.unref();
172-
socket.setKeepAlive(agent.keepAliveMsecs);
173+
return true;
173174
```
174175

175176
This method can be overridden by a particular `Agent` subclass. If this
@@ -226,13 +227,14 @@ added: v0.11.4
226227
* `port` {number} Port of remote server
227228
* `localAddress` {string} Local interface to bind for network connections
228229
when issuing the request
230+
* `family` {integer} Must be 4 or 6 if this doesn't equal `undefined`.
229231
* Returns: {string}
230232

231233
Get a unique name for a set of request options, to determine whether a
232-
connection can be reused. For an HTTP agent, this returns
233-
`host:port:localAddress`. For an HTTPS agent, the name includes the
234-
CA, cert, ciphers, and other HTTPS/TLS-specific options that determine
235-
socket reusability.
234+
connection can be reused. For an HTTP agent, this returns
235+
`host:port:localAddress` or `host:port:localAddress:family`. For an HTTPS agent,
236+
the name includes the CA, cert, ciphers, and other HTTPS/TLS-specific options
237+
that determine socket reusability.
236238

237239
### agent.maxFreeSockets
238240
<!-- YAML
@@ -253,8 +255,7 @@ added: v0.3.6
253255
* {number}
254256

255257
By default set to Infinity. Determines how many concurrent sockets the agent
256-
can have open per origin. Origin is either a 'host:port' or
257-
'host:port:localAddress' combination.
258+
can have open per origin. Origin is the returned value of [`agent.getName()`][].
258259

259260
### agent.requests
260261
<!-- YAML
@@ -285,7 +286,7 @@ This object is created internally and returned from [`http.request()`][]. It
285286
represents an _in-progress_ request whose header has already been queued. The
286287
header is still mutable using the `setHeader(name, value)`, `getHeader(name)`,
287288
`removeHeader(name)` API. The actual header will be sent along with the first
288-
data chunk or when closing the connection.
289+
data chunk or when calling [`request.end()`][].
289290

290291
To get the response, add a listener for [`'response'`][] to the request object.
291292
[`'response'`][] will be emitted from the request object when the response
@@ -590,11 +591,17 @@ Example:
590591

591592
```js
592593
const http = require('http');
593-
const server = http.createServer((req, res) => {
594-
const ip = req.socket.remoteAddress;
595-
const port = req.socket.remotePort;
596-
res.end(`Your IP address is ${ip} and your source port is ${port}.`);
597-
}).listen(3000);
594+
const options = {
595+
host: 'www.google.com',
596+
};
597+
const req = http.get(options);
598+
req.end();
599+
req.once('response', (res) => {
600+
const ip = req.socket.localAddress;
601+
const port = req.socket.localPort;
602+
console.log(`Your IP address is ${ip} and your source port is ${port}.`);
603+
// consume response object
604+
});
598605
```
599606

600607
### request.write(chunk[, encoding][, callback])
@@ -652,7 +659,7 @@ not be emitted.
652659
added: v5.5.0
653660
-->
654661

655-
* `request` {http.ClientRequest}
662+
* `request` {http.IncomingMessage}
656663
* `response` {http.ServerResponse}
657664

658665
Emitted each time a request with an HTTP `Expect` header is received, where the
@@ -1224,8 +1231,8 @@ Example:
12241231
```js
12251232
const http = require('http');
12261233
const server = http.createServer((req, res) => {
1227-
const ip = req.socket.remoteAddress;
1228-
const port = req.socket.remotePort;
1234+
const ip = res.socket.remoteAddress;
1235+
const port = res.socket.remotePort;
12291236
res.end(`Your IP address is ${ip} and your source port is ${port}.`);
12301237
}).listen(3000);
12311238
```
@@ -1883,6 +1890,7 @@ const req = http.request(options, (res) => {
18831890
[`TypeError`]: errors.html#errors_class_typeerror
18841891
[`URL`]: url.html#url_the_whatwg_url_api
18851892
[`agent.createConnection()`]: #http_agent_createconnection_options_callback
1893+
[`agent.getName()`]: #http_agent_getname_options
18861894
[`destroy()`]: #http_agent_destroy
18871895
[`http.Agent`]: #http_class_http_agent
18881896
[`http.ClientRequest`]: #http_class_http_clientrequest
@@ -1898,6 +1906,7 @@ const req = http.request(options, (res) => {
18981906
[`net.Server`]: net.html#net_class_net_server
18991907
[`net.Socket`]: net.html#net_class_net_socket
19001908
[`net.createConnection()`]: net.html#net_net_createconnection_options_connectlistener
1909+
[`request.end()`]: #http_request_end_data_encoding_callback
19011910
[`request.socket`]: #http_request_socket
19021911
[`request.socket.getPeerCertificate()`]: tls.html#tls_tlssocket_getpeercertificate_detailed
19031912
[`request.write(data, encoding)`]: #http_request_write_chunk_encoding_callback

0 commit comments

Comments
 (0)