Reputation: 368
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();
});
Upvotes: 5
Views: 4970
Reputation: 6764
You posted the solution in your question.
Stops the server from accepting new connections and keeps existing connections.
In other words, connections that you have opened are kept open until they time out. This is called a keep-alive
connection.
See this answer how-do-i-shutdown-a-node-js-https-server-immediately for a way to close all your connections immediately after calling the server.close();
method.
Upvotes: 3