为什么我在Node.js中从10个外部http请求中获得8个响应

时间:2013-07-25 22:15:47

标签: node.js request response

My Node.js应用程序在将处理过的信息发送给客户端之前,会生成两种类型的http请求服务器端:

  • 第一种类型的服务器端请求获取从外部API获取的链接列表(仅1个请求);
  • 第二种类型的服务器端请求获取网页的html(重复10次,因为API返回的列表包含10个URL)。

即使我看到请求了10个网址,也只会生成8个响应。这是代码:

app.get('/search', function(clientReq, clientRes) {
    console.log(clientReq.query.r);
    // first type of request to retrieve the json data containing 10 URLs
    var searchReq = https.request({ host: 'myhost.com', path: '/?q=' + clientReq.query.r }, function(searchRes) {
        console.log('searching...');
        var searchOutput = '';
        var communications = [];
        var waiting = 0;

        searchRes.on('data', function(d) {
            searchOutput += d;
        });

        searchRes.on('end', function() {
            var obj = JSON.parse(searchOutput);
            for(var i = 0; i < obj.results.length; i++){
                waiting++;
                console.log(encodeURIComponent(obj.results[i].uniqueId)); // prints 10 URLs
                // second type of request, fetches a web page listed in searchOutput
                var fetchReq = https.request({ host: 'myhost.com', path: '/?uniqueId='+ encodeURIComponent(obj.results[i].uniqueId) }, function(fetchRes) {
                    var htmlOutput = '';
                    console.log("starting to read the fetch response"); // only logs 8 times
                    fetchRes.on('data', function(d) {
                        htmlOutput += d;
                        if (htmlOutput.toString().indexOf("<div>") !== -1) {
                            var communication = "";
                            var $ = cheerio.load(htmlOutput.toString().substring(htmlOutput.toString().indexOf("<body "),htmlOutput.toString().indexOf("<div>")));
                            $('p').each(function (index) {
                                communication += $(this).text();
                            });
                            communications.push({"x":communication});
                            fetchRes.destroy();
                            waiting--;
                            complete();
                        }
                    });
                    fetchRes.on('end', function() {
                        console.log('this is the end of the fetch response'); // only logs 8 times
                    });   

                    fetchRes.on('error', function(e) {
                        console.error(e);
                    });
                });
                fetchReq.end(console.log("fetchReq ended")); // prints 10 times
                fetchReq.on('error', function(e) {
                    console.error(e);
                });  
            };    
            function complete() {
                if(waiting === 0) {
                    clientRes.send(communications);
                }
            }
        });
    });

    searchReq.end();

    searchReq.on('error', function(e) {
        console.error(e);
    });
});

我使用fetchRes.destroy();,因为HTML可能很大,我只需要它的顶部。

这是我的控制台中的输出,从第10个fetchReq ended开始:

fetchReq ended
starting to read the fetch response
this is the end of the fetch response
starting to read the fetch response
this is the end of the fetch response
starting to read the fetch response
this is the end of the fetch response
starting to read the fetch response
this is the end of the fetch response
starting to read the fetch response
this is the end of the fetch response
starting to read the fetch response
this is the end of the fetch response
starting to read the fetch response
this is the end of the fetch response
starting to read the fetch response
this is the end of the fetch response

如您所见,我只收到8条回复。最终结果是communications永远不会发送回浏览器,因为waiting未到达0

0 个答案:

没有答案