Skip to content

Commit 29139bf

Browse files
phillipjMylesBorins
authored andcommitted
doc: improve server.listen() random port
Minor rewording related to making a server listen to a random port, and added how to retrieve which port was randomly chosen by the OS. Also changed documented `server.listen()` signature as it does in fact not require `port` to be provided. PR-URL: #8025 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent c41efe4 commit 29139bf

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

doc/api/http.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -640,15 +640,17 @@ Start a UNIX socket server listening for connections on the given `path`.
640640
This function is asynchronous. `callback` will be added as a listener for the
641641
`'listening'` event. See also [`net.Server.listen(path)`][].
642642

643-
### server.listen(port[, hostname][, backlog][, callback])
643+
### server.listen([port][, hostname][, backlog][, callback])
644644
<!-- YAML
645645
added: v0.1.90
646646
-->
647647

648648
Begin accepting connections on the specified `port` and `hostname`. If the
649649
`hostname` is omitted, the server will accept connections on any IPv6 address
650-
(`::`) when IPv6 is available, or any IPv4 address (`0.0.0.0`) otherwise. Use a
651-
port value of `0` to have the operating system assign an available port.
650+
(`::`) when IPv6 is available, or any IPv4 address (`0.0.0.0`) otherwise.
651+
Omit the port argument, or use a port value of `0`, to have the operating system
652+
assign a random port, which can be retrieved by using `server.address().port`
653+
after the `'listening'` event has been emitted.
652654

653655
To listen to a unix socket, supply a filename instead of port and hostname.
654656

doc/api/net.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ var server = net.createServer((socket) => {
7373

7474
// grab a random port.
7575
server.listen(() => {
76-
address = server.address();
77-
console.log('opened server on %j', address);
76+
console.log('opened server on', server.address());
7877
});
7978
```
8079

@@ -140,7 +139,7 @@ The last parameter `callback` will be added as a listener for the
140139
[`'listening'`][] event.
141140

142141
The parameter `backlog` behaves the same as in
143-
[`server.listen(port[, hostname][, backlog][, callback])`][`server.listen(port, host, backlog, callback)`].
142+
[`server.listen([port][, hostname][, backlog][, callback])`][`server.listen(port, host, backlog, callback)`].
144143

145144
### server.listen(options[, callback])
146145
<!-- YAML
@@ -157,7 +156,7 @@ added: v0.11.14
157156

158157
The `port`, `host`, and `backlog` properties of `options`, as well as the
159158
optional callback function, behave as they do on a call to
160-
[`server.listen(port[, hostname][, backlog][, callback])`][`server.listen(port, host, backlog, callback)`].
159+
[`server.listen([port][, hostname][, backlog][, callback])`][`server.listen(port, host, backlog, callback)`].
161160
Alternatively, the `path` option can be used to specify a UNIX socket.
162161

163162
If `exclusive` is `false` (default), then cluster workers will use the same
@@ -209,17 +208,19 @@ double-backslashes, such as:
209208
path.join('\\\\?\\pipe', process.cwd(), 'myctl'))
210209

211210
The parameter `backlog` behaves the same as in
212-
[`server.listen(port[, hostname][, backlog][, callback])`][`server.listen(port, host, backlog, callback)`].
211+
[`server.listen([port][, hostname][, backlog][, callback])`][`server.listen(port, host, backlog, callback)`].
213212

214-
### server.listen(port[, hostname][, backlog][, callback])
213+
### server.listen([port][, hostname][, backlog][, callback])
215214
<!-- YAML
216215
added: v0.1.90
217216
-->
218217

219218
Begin accepting connections on the specified `port` and `hostname`. If the
220219
`hostname` is omitted, the server will accept connections on any IPv6 address
221-
(`::`) when IPv6 is available, or any IPv4 address (`0.0.0.0`) otherwise. Use a
222-
port value of `0` to have the operating system assign an available port.
220+
(`::`) when IPv6 is available, or any IPv4 address (`0.0.0.0`) otherwise.
221+
Omit the port argument, or use a port value of `0`, to have the operating system
222+
assign a random port, which can be retrieved by using `server.address().port`
223+
after the `'listening'` event has been emitted.
223224

224225
Backlog is the maximum length of the queue of pending connections.
225226
The actual length will be determined by the OS through sysctl settings such as

0 commit comments

Comments
 (0)