According to the documentation calling server.close()
Stops the server from accepting new connections and keeps existing connections.
So my code is:
var http = require('http');
var server = http.createServer(function (req, res) {
console.log('path: ' + req.url);
server.getConnections(function (err, count) {
console.log('connections: ' + count);
res.end('end');
server.close();
});
});
server.listen(3000);
server.on('close', function () {
console.log('server closed');
process.exit(1);
});
Here is what I get after making two requests to http://localhost:3000
:
> path: / connections: 1
> path: / connections: 1
>
> net.js:1236
> throw new Error('Not running');
> ^ Error: Not running
> at Server.close (net.js:1236:11)
This means that the second connection is being accepted even though during the first one I have called server.close()
.
What am I missing?
EDIT: As per @gino9's comment even if I close the server outside the getConnections
callback, the problem remains.
var server = http.createServer(function (req, res) {
console.log('path: ' + req.url);
res.end(new Date() + '');
server.close();
});