diff --git a/doc/api/http.markdown b/doc/api/http.markdown index 74941cc57896bb..4383512a7e66c1 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -548,6 +548,11 @@ parameter is 511 (not 512). This function is asynchronous. The last parameter `callback` will be added as a listener for the `'listening'` event. See also [`net.Server.listen(port)`][]. +### server.listening + +A Boolean indicating whether or not the server is listening for +connections. + ### server.maxHeadersCount Limits maximum incoming headers count, equal to 1000 by default. If set to 0 - diff --git a/doc/api/net.markdown b/doc/api/net.markdown index 7e898596f5a18e..6ee97fb379de3c 100644 --- a/doc/api/net.markdown +++ b/doc/api/net.markdown @@ -196,6 +196,11 @@ would be to wait a second and then try again. This can be done with (Note: All sockets in Node.js set `SO_REUSEADDR` already) +### server.listening + +A Boolean indicating whether or not the server is listening for +connections. + ### server.maxConnections Set this property to reject connections when the server's connection count gets diff --git a/lib/net.js b/lib/net.js index 68b79462447cc4..636915d363ded6 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1384,6 +1384,14 @@ Server.prototype.listen = function() { return self; }; +Object.defineProperty(Server.prototype, 'listening', { + get: function() { + return !!this._handle; + }, + configurable: true, + enumerable: true +}); + Server.prototype.address = function() { if (this._handle && this._handle.getsockname) { var out = {}; diff --git a/test/parallel/test-http-listening.js b/test/parallel/test-http-listening.js new file mode 100644 index 00000000000000..efa6873345a8b2 --- /dev/null +++ b/test/parallel/test-http-listening.js @@ -0,0 +1,16 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); + +const server = http.createServer(); + +assert.strictEqual(server.listening, false); + +server.listen(common.PORT, common.mustCall(() => { + assert.strictEqual(server.listening, true); + + server.close(common.mustCall(() => { + assert.strictEqual(server.listening, false); + })); +})); diff --git a/test/parallel/test-net-listening.js b/test/parallel/test-net-listening.js new file mode 100644 index 00000000000000..a0efb099b3f11f --- /dev/null +++ b/test/parallel/test-net-listening.js @@ -0,0 +1,16 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); + +const server = net.createServer(); + +assert.strictEqual(server.listening, false); + +server.listen(common.PORT, common.mustCall(() => { + assert.strictEqual(server.listening, true); + + server.close(common.mustCall(() => { + assert.strictEqual(server.listening, false); + })); +}));