根据文档调用
这意味着即使在第一次连接期间我调用了
server.close()
将会:
因此我的代码如下:停止服务器接收新的连接,但维持已有的连接。
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);
});
在向 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)
这意味着即使在第一次连接期间我调用了
server.close()
,第二个连接仍然被接受。
我错过了什么吗?
编辑:根据 @gino9 的评论,即使在 getConnections
回调之外关闭服务器,问题仍然存在。
var server = http.createServer(function (req, res) {
console.log('path: ' + req.url);
res.end(new Date() + '');
server.close();
});
server.close()
移到外面并不能解决这个问题。 - simich