From 18d24e60c5e38b2c5823ed057e54145f1847e085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Moreira?= Date: Mon, 18 Jan 2016 13:36:48 +0000 Subject: [PATCH] net: add net.listening boolean property over a getter Added a listening property into net.Server.prototype indicating if the server is listening or not for connections. Other Server constructors that rely on net.Server should also gain access to this property. Also included tests for net and http subsystems. PR-URL: https://github.com/nodejs/node/pull/4743 Reviewed-By: Evan Lucas Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- doc/api/http.markdown | 5 +++++ doc/api/net.markdown | 5 +++++ lib/net.js | 8 ++++++++ test/parallel/test-http-listening.js | 16 ++++++++++++++++ test/parallel/test-net-listening.js | 16 ++++++++++++++++ 5 files changed, 50 insertions(+) create mode 100644 test/parallel/test-http-listening.js create mode 100644 test/parallel/test-net-listening.js diff --git a/doc/api/http.markdown b/doc/api/http.markdown index 298aa64bb03c9a..5a0898effafc7f 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -555,6 +555,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 2cc9870c6672ac..1cd14b563fc439 100644 --- a/doc/api/net.markdown +++ b/doc/api/net.markdown @@ -203,6 +203,11 @@ server.on('error', (e) => { (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 157b197fca31c9..3e08d00f7ec65c 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1377,6 +1377,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); + })); +}));