Skip to content

Commit 46756ac

Browse files
JustinBeckwithaddaleax
authored andcommittedJul 18, 2017
doc: document res.connection and res.socket
Adds documentation and samples for the `connection` and `socket` properties available on the `http.serverResponse` and `http.clientRequest` objects. PR-URL: #13617 Fixes: #12617 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Alexey Orlenko <[email protected]>
1 parent 6e30e25 commit 46756ac

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
 

‎doc/api/http.md

+69
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,15 @@ added: v0.11.14
496496
If a request has been aborted, this value is the time when the request was
497497
aborted, in milliseconds since 1 January 1970 00:00:00 UTC.
498498

499+
### request.connection
500+
<!-- YAML
501+
added: v0.3.0
502+
-->
503+
504+
* {net.Socket}
505+
506+
See [`request.socket`][]
507+
499508
### request.end([data[, encoding]][, callback])
500509
<!-- YAML
501510
added: v0.1.90
@@ -564,6 +573,30 @@ Once a socket is assigned to this request and is connected
564573

565574
Returns `request`.
566575

576+
### request.socket
577+
<!-- YAML
578+
added: v0.3.0
579+
-->
580+
581+
* {net.Socket}
582+
583+
Reference to the underlying socket. Usually users will not want to access
584+
this property. In particular, the socket will not emit `'readable'` events
585+
because of how the protocol parser attaches to the socket. After
586+
`response.end()`, the property is nulled. The `socket` may also be accessed
587+
via `request.connection`.
588+
589+
Example:
590+
591+
```js
592+
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);
598+
```
599+
567600
### request.write(chunk[, encoding][, callback])
568601
<!-- YAML
569602
added: v0.1.29
@@ -955,6 +988,16 @@ response.end();
955988
Attempting to set a header field name or value that contains invalid characters
956989
will result in a [`TypeError`][] being thrown.
957990

991+
992+
### response.connection
993+
<!-- YAML
994+
added: v0.3.0
995+
-->
996+
997+
* {net.Socket}
998+
999+
See [`response.socket`][].
1000+
9581001
### response.end([data][, encoding][, callback])
9591002
<!-- YAML
9601003
added: v0.1.90
@@ -1163,6 +1206,30 @@ timed out sockets must be handled explicitly.
11631206

11641207
Returns `response`.
11651208

1209+
### response.socket
1210+
<!-- YAML
1211+
added: v0.3.0
1212+
-->
1213+
1214+
* {net.Socket}
1215+
1216+
Reference to the underlying socket. Usually users will not want to access
1217+
this property. In particular, the socket will not emit `'readable'` events
1218+
because of how the protocol parser attaches to the socket. After
1219+
`response.end()`, the property is nulled. The `socket` may also be accessed
1220+
via `response.connection`.
1221+
1222+
Example:
1223+
1224+
```js
1225+
const http = require('http');
1226+
const server = http.createServer((req, res) => {
1227+
const ip = req.socket.remoteAddress;
1228+
const port = req.socket.remotePort;
1229+
res.end(`Your IP address is ${ip} and your source port is ${port}.`);
1230+
}).listen(3000);
1231+
```
1232+
11661233
### response.statusCode
11671234
<!-- YAML
11681235
added: v0.4.0
@@ -1831,9 +1898,11 @@ const req = http.request(options, (res) => {
18311898
[`net.Server`]: net.html#net_class_net_server
18321899
[`net.Socket`]: net.html#net_class_net_socket
18331900
[`net.createConnection()`]: net.html#net_net_createconnection_options_connectlistener
1901+
[`request.socket`]: #http_request_socket
18341902
[`request.socket.getPeerCertificate()`]: tls.html#tls_tlssocket_getpeercertificate_detailed
18351903
[`response.end()`]: #http_response_end_data_encoding_callback
18361904
[`response.setHeader()`]: #http_response_setheader_name_value
1905+
[`response.socket`]: #http_response_socket
18371906
[`response.write()`]: #http_response_write_chunk_encoding_callback
18381907
[`response.write(data, encoding)`]: #http_response_write_chunk_encoding_callback
18391908
[`response.writeContinue()`]: #http_response_writecontinue

0 commit comments

Comments
 (0)
Please sign in to comment.