My Node.js应用程序在将处理过的信息发送给客户端之前,会生成两种类型的http请求服务器端:
即使我看到请求了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
。