小编sca*_*ald的帖子

node.js http.get在对远程站点的5个请求后挂起

我正在编写一个简单的api端点来确定我的服务器是否能够访问互联网.它运行良好,但在5个请求(每次正好5个)之后,请求挂起.当我将Google切换到Hotmail.com时会发生同样的事情,这让我认为这是我的最终目的.我需要关闭http.get请求吗?我的印象是这个函数会自动关闭请求.

// probably a poor assumption, but if Google is unreachable its generally safe to say     that the server can't access the internet
// using this client side in the dashboard to enable/disable internet resources

app.get('/api/internetcheck', function(req, res) {
console.log("trying google...");
    http.get("http://www.google.com", function(r){
        console.log("Got status code!: " +r.statusCode.toString());
        res.send(r.statusCode.toString());
        res.end();
        console.log("ended!"); 
    }).on('error', function(e) {
        console.log("Got error: " + e.message);
    });
});
Run Code Online (Sandbox Code Playgroud)

http node.js express

32
推荐指数
1
解决办法
1万
查看次数

标签 统计

express ×1

http ×1

node.js ×1