Browse Source

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 <evanlucas@me.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
process-exit-stdio-flushing
José Moreira 9 years ago
committed by Evan Lucas
parent
commit
5ef9989bd6
  1. 5
      doc/api/http.markdown
  2. 5
      doc/api/net.markdown
  3. 8
      lib/net.js
  4. 16
      test/parallel/test-http-listening.js
  5. 16
      test/parallel/test-net-listening.js

5
doc/api/http.markdown

@ -560,6 +560,11 @@ parameter is 511 (not 512).
This function is asynchronous. The last parameter `callback` will be added as This function is asynchronous. The last parameter `callback` will be added as
a listener for the `'listening'` event. See also [`net.Server.listen(port)`][]. 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 ### server.maxHeadersCount
Limits maximum incoming headers count, equal to 1000 by default. If set to 0 - Limits maximum incoming headers count, equal to 1000 by default. If set to 0 -

5
doc/api/net.markdown

@ -202,6 +202,11 @@ server.on('error', (e) => {
(Note: All sockets in Node.js set `SO_REUSEADDR` already) (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 ### server.maxConnections
Set this property to reject connections when the server's connection count gets Set this property to reject connections when the server's connection count gets

8
lib/net.js

@ -1389,6 +1389,14 @@ Server.prototype.listen = function() {
return self; return self;
}; };
Object.defineProperty(Server.prototype, 'listening', {
get: function() {
return !!this._handle;
},
configurable: true,
enumerable: true
});
Server.prototype.address = function() { Server.prototype.address = function() {
if (this._handle && this._handle.getsockname) { if (this._handle && this._handle.getsockname) {
var out = {}; var out = {};

16
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);
}));
}));

16
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);
}));
}));
Loading…
Cancel
Save