为什么我无法通过在浏览器中请求 localhost:13777/close 来关闭服务器(它继续接受新请求),但它会在超时 15000 时正常关闭? Node 版本是0.10.18。我陷入了这个问题,尝试使用文档中有关域异常处理的代码示例(每次我第二次尝试请求错误页面时,它都会给我“未运行”错误),最后来到此代码。
var server
server = require("http").createServer(function(req,res){
if(req.url == "/close")
{
console.log("Closing server (no timeout)")
setTimeout(function(){
console.log("I'm the timeout")
}, 5000);
server.close(function(){
console.log("Server closed (no timeout)")
})
res.end('closed');
}
else
{
res.end('ok');
}
});
server.listen(13777,function(){console.log("Server listening")});
setTimeout(function(){
console.log("Closing server (timeout 15000)")
server.close(function(){console.log("Server closed (timeout 15000)")})
}, 15000);
最佳答案
服务器仍在等待客户端的请求。客户端正在利用 HTTP keep-alive。
我想您会发现,虽然现有客户端可以发出新请求(因为连接已经建立),但其他客户端将无法这样做。
关于javascript - 无法关闭服务器(nodeJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18941003/