@@ -74,9 +74,9 @@ to keep the Node.js process running when there are no outstanding requests.
74
74
It is good practice, to [ ` destroy() ` ] [ ] an ` Agent ` instance when it is no
75
75
longer in use, because unused sockets consume OS resources.
76
76
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
78
78
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
80
80
like the following may be done:
81
81
82
82
``` js
@@ -168,8 +168,9 @@ Called when `socket` is detached from a request and could be persisted by the
168
168
Agent. Default behavior is to:
169
169
170
170
``` js
171
+ socket .setKeepAlive (true , this .keepAliveMsecs );
171
172
socket .unref ();
172
- socket . setKeepAlive ( agent . keepAliveMsecs ) ;
173
+ return true ;
173
174
```
174
175
175
176
This method can be overridden by a particular ` Agent ` subclass. If this
@@ -226,13 +227,14 @@ added: v0.11.4
226
227
* ` port ` {number} Port of remote server
227
228
* ` localAddress ` {string} Local interface to bind for network connections
228
229
when issuing the request
230
+ * ` family ` {integer} Must be 4 or 6 if this doesn't equal ` undefined ` .
229
231
* Returns: {string}
230
232
231
233
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.
236
238
237
239
### agent.maxFreeSockets
238
240
<!-- YAML
@@ -253,8 +255,7 @@ added: v0.3.6
253
255
* {number}
254
256
255
257
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() ` ] [ ] .
258
259
259
260
### agent.requests
260
261
<!-- YAML
@@ -285,7 +286,7 @@ This object is created internally and returned from [`http.request()`][]. It
285
286
represents an _ in-progress_ request whose header has already been queued. The
286
287
header is still mutable using the ` setHeader(name, value) ` , ` getHeader(name) ` ,
287
288
` 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() ` ] [ ] .
289
290
290
291
To get the response, add a listener for [ ` 'response' ` ] [ ] to the request object.
291
292
[ ` 'response' ` ] [ ] will be emitted from the request object when the response
@@ -590,11 +591,17 @@ Example:
590
591
591
592
``` js
592
593
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
+ });
598
605
```
599
606
600
607
### request.write(chunk[ , encoding] [ , callback ] )
@@ -652,7 +659,7 @@ not be emitted.
652
659
added: v5.5.0
653
660
-->
654
661
655
- * ` request ` {http.ClientRequest }
662
+ * ` request ` {http.IncomingMessage }
656
663
* ` response ` {http.ServerResponse}
657
664
658
665
Emitted each time a request with an HTTP ` Expect ` header is received, where the
@@ -1224,8 +1231,8 @@ Example:
1224
1231
``` js
1225
1232
const http = require (' http' );
1226
1233
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 ;
1229
1236
res .end (` Your IP address is ${ ip} and your source port is ${ port} .` );
1230
1237
}).listen (3000 );
1231
1238
```
@@ -1883,6 +1890,7 @@ const req = http.request(options, (res) => {
1883
1890
[ `TypeError` ] : errors.html#errors_class_typeerror
1884
1891
[ `URL` ] : url.html#url_the_whatwg_url_api
1885
1892
[ `agent.createConnection()` ] : #http_agent_createconnection_options_callback
1893
+ [ `agent.getName()` ] : #http_agent_getname_options
1886
1894
[ `destroy()` ] : #http_agent_destroy
1887
1895
[ `http.Agent` ] : #http_class_http_agent
1888
1896
[ `http.ClientRequest` ] : #http_class_http_clientrequest
@@ -1898,6 +1906,7 @@ const req = http.request(options, (res) => {
1898
1906
[ `net.Server` ] : net.html#net_class_net_server
1899
1907
[ `net.Socket` ] : net.html#net_class_net_socket
1900
1908
[ `net.createConnection()` ] : net.html#net_net_createconnection_options_connectlistener
1909
+ [ `request.end()` ] : #http_request_end_data_encoding_callback
1901
1910
[ `request.socket` ] : #http_request_socket
1902
1911
[ `request.socket.getPeerCertificate()` ] : tls.html#tls_tlssocket_getpeercertificate_detailed
1903
1912
[ `request.write(data, encoding)` ] : #http_request_write_chunk_encoding_callback
0 commit comments