diff --git a/doc/api/http.md b/doc/api/http.md index 8bd8da55701b16..d263068bd68fc0 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -691,15 +691,17 @@ Start a UNIX socket server listening for connections on the given `path`. This function is asynchronous. `callback` will be added as a listener for the [`'listening'`][] event. See also [`net.Server.listen(path)`][]. -### server.listen(port[, hostname][, backlog][, callback]) +### server.listen([port][, hostname][, backlog][, callback]) Begin accepting connections on the specified `port` and `hostname`. If the `hostname` is omitted, the server will accept connections on any IPv6 address -(`::`) when IPv6 is available, or any IPv4 address (`0.0.0.0`) otherwise. Use a -port value of `0` to have the operating system assign an available port. +(`::`) when IPv6 is available, or any IPv4 address (`0.0.0.0`) otherwise. +Omit the port argument, or use a port value of `0`, to have the operating system +assign a random port, which can be retrieved by using `server.address().port` +after the `'listening'` event has been emitted. To listen to a unix socket, supply a filename instead of port and hostname. diff --git a/doc/api/net.md b/doc/api/net.md index 18090093ea038d..d58d00edbdba2b 100644 --- a/doc/api/net.md +++ b/doc/api/net.md @@ -73,8 +73,7 @@ var server = net.createServer((socket) => { // grab a random port. server.listen(() => { - address = server.address(); - console.log('opened server on %j', address); + console.log('opened server on', server.address()); }); ``` @@ -140,7 +139,7 @@ The last parameter `callback` will be added as a listener for the [`'listening'`][] event. The parameter `backlog` behaves the same as in -[`server.listen(port[, hostname][, backlog][, callback])`][`server.listen(port, host, backlog, callback)`]. +[`server.listen([port][, hostname][, backlog][, callback])`][`server.listen(port, host, backlog, callback)`]. ### server.listen(options[, callback]) Begin accepting connections on the specified `port` and `hostname`. If the `hostname` is omitted, the server will accept connections on any IPv6 address -(`::`) when IPv6 is available, or any IPv4 address (`0.0.0.0`) otherwise. Use a -port value of `0` to have the operating system assign an available port. +(`::`) when IPv6 is available, or any IPv4 address (`0.0.0.0`) otherwise. +Omit the port argument, or use a port value of `0`, to have the operating system +assign a random port, which can be retrieved by using `server.address().port` +after the `'listening'` event has been emitted. Backlog is the maximum length of the queue of pending connections. The actual length will be determined by the OS through sysctl settings such as