Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: fix some mistakes of http-api-doc #14625

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ to keep the Node.js process running when there are no outstanding requests.
It is good practice, to [`destroy()`][] an `Agent` instance when it is no
longer in use, because unused sockets consume OS resources.

Sockets are removed from an agent's pool when the socket emits either
Sockets are removed from an agent when the socket emits either
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agent's pool is agent.freeSockets. When a socket is assigned to a request, the socket must not be in agent.freeSockets.

a `'close'` event or an `'agentRemove'` event. When intending to keep one
HTTP request open for a long time without keeping it in the pool, something
HTTP request open for a long time without keeping it in the agent, something
like the following may be done:

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

```js
socket.setKeepAlive(true, this.keepAliveMsecs);
socket.unref();
socket.setKeepAlive(agent.keepAliveMsecs);
return true;
```

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

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

### agent.maxFreeSockets
<!-- YAML
Expand All @@ -253,8 +255,7 @@ added: v0.3.6
* {number}

By default set to Infinity. Determines how many concurrent sockets the agent
can have open per origin. Origin is either a 'host:port' or
'host:port:localAddress' combination.
can have open per origin. Origin is the returned value of [`agent.getName()`][].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link needs a reference at the bottom of the doc.


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link needs a reference at the bottom of the doc.


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

```js
const http = require('http');
const server = http.createServer((req, res) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The req in the code is not http.ClientRequest.

const ip = req.socket.remoteAddress;
const port = req.socket.remotePort;
res.end(`Your IP address is ${ip} and your source port is ${port}.`);
}).listen(3000);
const options = {
host: 'www.google.com',
};
const req = http.get(options);
req.end();
req.once('response', (res) => {
const ip = req.socket.localAddress;
const port = req.socket.localPort;
console.log(`Your IP address is ${ip} and your source port is ${port}.`);
// consume response object
});
```

### request.write(chunk[, encoding][, callback])
Expand Down Expand Up @@ -652,7 +659,7 @@ not be emitted.
added: v5.5.0
-->

* `request` {http.ClientRequest}
* `request` {http.IncomingMessage}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell @vsemozhetbyt It's changed after you reviewed.

* `response` {http.ServerResponse}

Emitted each time a request with an HTTP `Expect` header is received, where the
Expand Down Expand Up @@ -1224,8 +1231,8 @@ Example:
```js
const http = require('http');
const server = http.createServer((req, res) => {
const ip = req.socket.remoteAddress;
const port = req.socket.remotePort;
const ip = res.socket.remoteAddress;
const port = res.socket.remotePort;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell @vsemozhetbyt It's changed after you reviewed.

res.end(`Your IP address is ${ip} and your source port is ${port}.`);
}).listen(3000);
```
Expand Down Expand Up @@ -1883,6 +1890,7 @@ const req = http.request(options, (res) => {
[`TypeError`]: errors.html#errors_class_typeerror
[`URL`]: url.html#url_the_whatwg_url_api
[`agent.createConnection()`]: #http_agent_createconnection_options_callback
[`agent.getName()`]: #http_agent_getname_options
[`destroy()`]: #http_agent_destroy
[`http.Agent`]: #http_class_http_agent
[`http.ClientRequest`]: #http_class_http_clientrequest
Expand All @@ -1898,6 +1906,7 @@ const req = http.request(options, (res) => {
[`net.Server`]: net.html#net_class_net_server
[`net.Socket`]: net.html#net_class_net_socket
[`net.createConnection()`]: net.html#net_net_createconnection_options_connectlistener
[`request.end()`]: #http_request_end_data_encoding_callback
[`request.socket`]: #http_request_socket
[`request.socket.getPeerCertificate()`]: tls.html#tls_tlssocket_getpeercertificate_detailed
[`request.write(data, encoding)`]: #http_request_write_chunk_encoding_callback
Expand Down