diff --git a/lib/net.js b/lib/net.js index d693b25d46..9c426102d6 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1451,11 +1451,12 @@ Server.prototype.listen = function(...args) { } // ([port][, host][, backlog][, cb]) where port is omitted, - // that is, listen() or listen(cb), - // or (options[, cb]) where options.port is explicitly set as undefined, - // bind to an arbitrary unused port + // that is, listen(), listen(null), listen(cb), or listen(null, cb) + // or (options[, cb]) where options.port is explicitly set as undefined or + // null, bind to an arbitrary unused port if (args.length === 0 || typeof args[0] === 'function' || - (typeof options.port === 'undefined' && 'port' in options)) { + (typeof options.port === 'undefined' && 'port' in options) || + options.port === null) { options.port = 0; } // ([port][, host][, backlog][, cb]) where port is specified diff --git a/test/parallel/test-net-server-listen-options.js b/test/parallel/test-net-server-listen-options.js index d2e70215dc..9f44a5bd3b 100644 --- a/test/parallel/test-net-server-listen-options.js +++ b/test/parallel/test-net-server-listen-options.js @@ -42,6 +42,7 @@ const listenOnPort = [ listen('0', common.mustCall(close)); listen(0, common.mustCall(close)); listen(undefined, common.mustCall(close)); + listen(null, common.mustCall(close)); // Test invalid ports assert.throws(() => listen(-1, common.mustNotCall()), portError); assert.throws(() => listen(NaN, common.mustNotCall()), portError); @@ -71,8 +72,6 @@ const listenOnPort = [ `expect listen(${util.inspect(options)}) to throw`); } - shouldFailToListen(null, { port: null }); - shouldFailToListen({ port: null }); shouldFailToListen(false, { port: false }); shouldFailToListen({ port: false }); shouldFailToListen(true, { port: true });