Skip to content

Commit

Permalink
net: add net.listening boolean property over a getter
Browse files Browse the repository at this point in the history
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: #4743
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
José Moreira authored and rvagg committed Feb 9, 2016
1 parent 411d813 commit 18d24e6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions doc/api/http.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down
5 changes: 5 additions & 0 deletions doc/api/net.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-http-listening.js
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions test/parallel/test-net-listening.js
Original file line number Diff line number Diff line change
@@ -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);
}));
}));

0 comments on commit 18d24e6

Please sign in to comment.