| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 484bfa2 commit b24e269
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -809,8 +809,9 @@ This function is asynchronous. `callback` will be added as a listener for the | |||
| 809 | 809 | ||
| 810 | 810 | Returns `server`. | |
| 811 | 811 | ||
| 812 | - *Note*: The `server.listen()` method may be called multiple times. Each | ||
| 813 | - subsequent call will *re-open* the server using the provided options. | ||
| 812 | + *Note*: The `server.listen()` method can be called again if and only if there was an error | ||
| 813 | + during the first `server.listen()` call or `server.close()` has been called. | ||
| 814 | + Otherwise, an `ERR_SERVER_ALREADY_LISTEN` error will be thrown. | ||
| 814 | 815 | ||
| 815 | 816 | ### server.listen(path[, callback]) | |
| 816 | 817 | <!-- YAML | |
@@ -825,8 +826,9 @@ Start a UNIX socket server listening for connections on the given `path`. | |||
| 825 | 826 | This function is asynchronous. `callback` will be added as a listener for the | |
| 826 | 827 | [`'listening'`][] event. See also [`net.Server.listen(path)`][]. | |
| 827 | 828 | ||
| 828 | - *Note*: The `server.listen()` method may be called multiple times. Each | ||
| 829 | - subsequent call will *re-open* the server using the provided options. | ||
| 829 | + *Note*: The `server.listen()` method can be called again if and only if there was an error | ||
| 830 | + during the first `server.listen()` call or `server.close()` has been called. | ||
| 831 | + Otherwise, an `ERR_SERVER_ALREADY_LISTEN` error will be thrown. | ||
| 830 | 832 | ||
| 831 | 833 | ### server.listen([port][, hostname][, backlog][, callback]) | |
| 832 | 834 | <!-- YAML | |
@@ -861,8 +863,9 @@ parameter is 511 (not 512). | |||
| 861 | 863 | This function is asynchronous. `callback` will be added as a listener for the | |
| 862 | 864 | [`'listening'`][] event. See also [`net.Server.listen(port)`][]. | |
| 863 | 865 | ||
| 864 | - *Note*: The `server.listen()` method may be called multiple times. Each | ||
| 865 | - subsequent call will *re-open* the server using the provided options. | ||
| 866 | + *Note*: The `server.listen()` method can be called again if and only if there was an error | ||
| 867 | + during the first `server.listen()` call or `server.close()` has been called. | ||
| 868 | + Otherwise, an `ERR_SERVER_ALREADY_LISTEN` error will be thrown. | ||
| 866 | 869 | ||
| 867 | 870 | ### server.listening | |
| 868 | 871 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -199,9 +199,9 @@ on Linux. The default value of this parameter is 511 (not 512). | |||
| 199 | 199 | ||
| 200 | 200 | * All [`net.Socket`][] are set to `SO_REUSEADDR` (See [socket(7)][] for | |
| 201 | 201 | details). | |
| 202 | - | ||
| 203 | - * The `server.listen()` method may be called multiple times. Each | ||
| 204 | - subsequent call will *re-open* the server using the provided options. | ||
| 202 | + * The `server.listen()` method can be called again if and only if there was an error | ||
| 203 | + during the first `server.listen()` call or `server.close()` has been called. | ||
| 204 | + Otherwise, an `ERR_SERVER_ALREADY_LISTEN` error will be thrown. | ||
| 205 | 205 | ||
| 206 | 206 | One of the most common errors raised when listening is `EADDRINUSE`. | |
| 207 | 207 | This happens when another server is already listening on the requested | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -237,6 +237,8 @@ E('ERR_NO_ICU', '%s is not supported on Node.js compiled without ICU'); | |||
| 237 | 237 | E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported'); | |
| 238 | 238 | E('ERR_OUTOFMEMORY', 'Out of memory'); | |
| 239 | 239 | E('ERR_PARSE_HISTORY_DATA', 'Could not parse history data in %s'); | |
| 240 | + E('ERR_SERVER_ALREADY_LISTEN', | ||
| 241 | + 'Listen method has been called more than once without closing.'); | ||
| 240 | 242 | E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound'); | |
| 241 | 243 | E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536'); | |
| 242 | 244 | E('ERR_SOCKET_BAD_TYPE', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1423,6 +1423,10 @@ Server.prototype.listen = function(...args) { | |||
| 1423 | 1423 | var options = normalized[0]; | |
| 1424 | 1424 | var cb = normalized[1]; | |
| 1425 | 1425 | ||
| 1426 | + if (this._handle) { | ||
| 1427 | + throw new errors.Error('ERR_SERVER_ALREADY_LISTEN'); | ||
| 1428 | + } | ||
| 1429 | + | ||
| 1426 | 1430 | var hasCallback = (cb !== null); | |
| 1427 | 1431 | if (hasCallback) { | |
| 1428 | 1432 | this.once('listening', cb); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,51 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const net = require('net'); | ||
| 6 | + | ||
| 7 | + // First test. Check that after error event you can listen right away. | ||
| 8 | + { | ||
| 9 | + const dummyServer = net.Server(); | ||
| 10 | + const server = net.Server(); | ||
| 11 | + | ||
| 12 | + // Run some server in order to simulate EADDRINUSE error. | ||
| 13 | + dummyServer.listen(common.mustCall(() => { | ||
| 14 | + // Try to listen used port. | ||
| 15 | + server.listen(dummyServer.address().port); | ||
| 16 | + })); | ||
| 17 | + | ||
| 18 | + server.on('error', common.mustCall((e) => { | ||
| 19 | + assert.doesNotThrow( | ||
| 20 | + () => server.listen(common.mustCall(() => { | ||
| 21 | + dummyServer.close(); | ||
| 22 | + server.close(); | ||
| 23 | + })) | ||
| 24 | + ); | ||
| 25 | + })); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + // Second test. Check that second listen call throws an error. | ||
| 29 | + { | ||
| 30 | + const server = net.Server(); | ||
| 31 | + | ||
| 32 | + server.listen(common.mustCall(() => server.close())); | ||
| 33 | + | ||
| 34 | + common.expectsError(() => server.listen(), { | ||
| 35 | + code: 'ERR_SERVER_ALREADY_LISTEN', | ||
| 36 | + type: Error | ||
| 37 | + }); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + // Third test. | ||
| 41 | + // Check that after the close call you can run listen method just fine. | ||
| 42 | + { | ||
| 43 | + const server = net.Server(); | ||
| 44 | + | ||
| 45 | + server.listen(common.mustCall(() => { | ||
| 46 | + server.close(); | ||
| 47 | + assert.doesNotThrow( | ||
| 48 | + () => server.listen(common.mustCall(() => server.close())) | ||
| 49 | + ); | ||
| 50 | + })); | ||
| 51 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments