Skip to content

Commit 18d24e6

Browse files
José Moreirarvagg
authored andcommitted
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: #4743 Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 411d813 commit 18d24e6

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

doc/api/http.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,11 @@ parameter is 511 (not 512).
555555
This function is asynchronous. The last parameter `callback` will be added as
556556
a listener for the `'listening'` event. See also [`net.Server.listen(port)`][].
557557

558+
### server.listening
559+
560+
A Boolean indicating whether or not the server is listening for
561+
connections.
562+
558563
### server.maxHeadersCount
559564

560565
Limits maximum incoming headers count, equal to 1000 by default. If set to 0 -

doc/api/net.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ server.on('error', (e) => {
203203

204204
(Note: All sockets in Node.js set `SO_REUSEADDR` already)
205205

206+
### server.listening
207+
208+
A Boolean indicating whether or not the server is listening for
209+
connections.
210+
206211
### server.maxConnections
207212

208213
Set this property to reject connections when the server's connection count gets

lib/net.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,14 @@ Server.prototype.listen = function() {
13771377
return self;
13781378
};
13791379

1380+
Object.defineProperty(Server.prototype, 'listening', {
1381+
get: function() {
1382+
return !!this._handle;
1383+
},
1384+
configurable: true,
1385+
enumerable: true
1386+
});
1387+
13801388
Server.prototype.address = function() {
13811389
if (this._handle && this._handle.getsockname) {
13821390
var out = {};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
6+
const server = http.createServer();
7+
8+
assert.strictEqual(server.listening, false);
9+
10+
server.listen(common.PORT, common.mustCall(() => {
11+
assert.strictEqual(server.listening, true);
12+
13+
server.close(common.mustCall(() => {
14+
assert.strictEqual(server.listening, false);
15+
}));
16+
}));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
6+
const server = net.createServer();
7+
8+
assert.strictEqual(server.listening, false);
9+
10+
server.listen(common.PORT, common.mustCall(() => {
11+
assert.strictEqual(server.listening, true);
12+
13+
server.close(common.mustCall(() => {
14+
assert.strictEqual(server.listening, false);
15+
}));
16+
}));

0 commit comments

Comments
 (0)