diff --git a/lib/net.js b/lib/net.js index 090ab9f0234dc0..e78e1b6f3fb942 100644 --- a/lib/net.js +++ b/lib/net.js @@ -68,6 +68,7 @@ const errors = require('internal/errors'); const { ERR_INVALID_ADDRESS_FAMILY, ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, ERR_INVALID_FD_TYPE, ERR_INVALID_IP_ADDRESS, ERR_INVALID_OPT_VALUE, @@ -1496,6 +1497,11 @@ Server.prototype.listen = function(...args) { return this; } + if (!(('port' in options) || ('path' in options))) { + throw new ERR_INVALID_ARG_VALUE('options', options, + 'must have the property "port" or "path"'); + } + throw new ERR_INVALID_OPT_VALUE('options', util.inspect(options)); }; diff --git a/test/parallel/test-net-server-listen-options.js b/test/parallel/test-net-server-listen-options.js index 4f7a6bd28f561a..41cc07d40d4d6e 100644 --- a/test/parallel/test-net-server-listen-options.js +++ b/test/parallel/test-net-server-listen-options.js @@ -57,12 +57,23 @@ const listenOnPort = [ const block = () => { net.createServer().listen(options, common.mustNotCall()); }; - common.expectsError(block, - { - code: 'ERR_INVALID_OPT_VALUE', - type: TypeError, - message: /^The value "{.*}" is invalid for option "options"$/ - }); + + if (typeof options === 'object' && + !(('port' in options) || ('path' in options))) { + common.expectsError(block, + { + code: 'ERR_INVALID_ARG_VALUE', + type: TypeError, + message: /^The argument 'options' must have the property "port" or "path"\. Received .+$/, + }); + } else { + common.expectsError(block, + { + code: 'ERR_INVALID_OPT_VALUE', + type: TypeError, + message: /^The value "{.*}" is invalid for option "options"(?:\. .+)?$/, + }); + } } shouldFailToListen(false, { port: false }); @@ -73,6 +84,11 @@ const listenOnPort = [ shouldFailToListen({ fd: -1 }); // Invalid path in listen(options) shouldFailToListen({ path: -1 }); - // Host without port + + // Neither port or path are specified in options + shouldFailToListen({}); shouldFailToListen({ host: 'localhost' }); + shouldFailToListen({ host: 'localhost:3000' }); + shouldFailToListen({ host: { port: 3000 } }); + shouldFailToListen({ exclusive: true }); }