Skip to content

Commit d185852

Browse files
committed
Add Server#close()
Fixes #31
1 parent 56a3676 commit d185852

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ A `port` value of zero will assign a random port.
6767

6868
`callback` will be added as an listener for the `'listening'` event.
6969

70+
## server.close([callback])
71+
Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the server is finally closed when all connections are ended and the server emits a 'close' event. The optional callback will be called once the 'close' event occurs.
72+
7073
## server.socket
7174
The underlying socket, returned by net.createServer or tls.createServer
7275

Server.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,17 @@ Server.prototype.listen = function (port, host, callback) {
9797
})
9898

9999
return this
100+
}
101+
102+
/**
103+
* Stops the server from accepting new connections and keeps existing connections.
104+
* This function is asynchronous, the server is finally closed when all connections are ended and the server emits a 'close' event.
105+
* The optional callback will be called once the 'close' event occurs.
106+
* @param {function()} [callback]
107+
*/
108+
Server.prototype.close = function (callback) {
109+
if (callback) {
110+
this.once('close', callback)
111+
}
112+
this.socket.close()
100113
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"test": "mocha -R spec -b"
2222
},
2323
"devDependencies": {
24-
"mocha": "^2.3.4",
25-
"should": "^8.1.0"
24+
"mocha": "^2.4.5",
25+
"should": "^8.3.1"
2626
}
2727
}

0 commit comments

Comments
 (0)