Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7fa4be0

Browse filesBrowse files
phillipjcjihrig
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: #7976 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 7c427bd commit 7fa4be0
Copy full SHA for 7fa4be0

File tree

Expand file treeCollapse file tree

2 files changed

+14
-11
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+14
-11
lines changed
Open diff view settings
Collapse file

‎doc/api/http.md‎

Copy file name to clipboardExpand all lines: doc/api/http.md
+5-3Lines changed: 5 additions & 3 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -691,15 +691,17 @@ Start a UNIX socket server listening for connections on the given `path`.
691691
This function is asynchronous. `callback` will be added as a listener for the
692692
[`'listening'`][] event. See also [`net.Server.listen(path)`][].
693693

694-
### server.listen(port[, hostname][, backlog][, callback])
694+
### server.listen([port][, hostname][, backlog][, callback])
695695
<!-- YAML
696696
added: v0.1.90
697697
-->
698698

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

704706
To listen to a unix socket, supply a filename instead of port and hostname.
705707

Collapse file

‎doc/api/net.md‎

Copy file name to clipboardExpand all lines: doc/api/net.md
+9-8Lines changed: 9 additions & 8 deletions
  • Display the source diff
  • Display the rich diff
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.