2016-04-29 356 views
0

发现几乎不可能将循环中http请求的响应作为数组捕获。我可以在console.log中看到数组,但是当我传递数组作为http服务器的响应时,我得到一个空数组。我做错了什么,或者有更好的方法来做到这一点?在nodejs循环中发出http请求

代码:

router.route('/uprns').post(function(request, response){ 
    response.setHeader('content-type', 'application/text'); 

    console.log('first element from the array is '+request.body.UPRNS[0]); 
    console.log('Number of items in array is '+request.body.UPRNS.length); 

if (request.body.UPRNS.length == 0) { 
     response.send('no UPRNS in request'); 
    } 

    var output = []; 
    var obj = ''; 

    for(var i = 0; i < request.body.UPRNS.length; i++) { 

    obj = request.body.UPRNS[i]; 

    //Make HTTP calls to  
    var options = { 
     host: 'orbisdigital.azure-api.net', 
     path: '/nosecurity/addresses?uprn='+obj // full URL as path 
    }; 

    callback = function(res) {  
     res.on('data', function (chunk) { 
     output.push(chunk.toString()); 
     }); 

     //the whole response has been recieved 
     res.on('end', function() { 
     console.log(output); 
     }); 
    } 

    Https.request(options, callback).end(); 
    } 

    response.send(output); 

}); 

我知道有很多谈论在for循环阻塞进程的,但对付在循环HTTP调用没有明确推荐的方式。 谢谢。

+0

输出数组中元素的顺序很重要?他是否应该遵守http请求的顺序? –

+0

在我的情况下,订单并不重要,但想知道如何实现订单。 – noexpert

回答

0

这是代码。查看添加评论的代码。通过node.js,here's a starter阅读异步编程。

router.route('/uprns').post(function (request, response) { 
    response.setHeader('content-type', 'application/text'); 
    console.log('first element from the array is ' + request.body.UPRNS[ 0 ]); // your 1st element in JSON array. 

    console.log('Number of items in array is ' + request.body.UPRNS.length); 
    var output = []; 
    var obj = ''; 

    for (var i = 0; i < request.body.UPRNS.length; i++) { 

     obj = request.body.UPRNS[ i ]; 

     console.log(obj); 

     //Make HTTP calls to 

     var options = { 
      host: 'orbisdigital.azure-api.net', 
      path: '/nosecurity/addresses?uprn=' + obj // full URL as path 
     }; 

     Https.request(options, callback).end(); 

    } 

    var countResponses = 0; 
    // Don't make functions in a loop, so I moved this function down 
    // here. 
    function callback(res) { 

     res.on('data', function (chunk) { 
      output.push(chunk.toString()); 
     }); 

     // Handles an error 
     request.on('error', function(err) { 
      console.error(err.stack); 
      response.statusCode = 500; // or what ever. 
      response.send(500, 'there was an error'); 
     }); 

     //the whole response has been recieved 
     res.on('end', function() { 
      console.log(output); 
      countResponses++; 
      if (countResponses === request.body.UPRNS.length) { 

       // Previously this code was executed directly 
       // after the loop finished. It did not wait for 
       // all the responses, so it sent the empty response. 
       // However, the other console.log(output) statements 
       // were called after this. 
       // 
       // There is a bug here that if request.body.UPRNS.length 
       // is zero, then the user will never get a response. I 
       // let you fix this up :). 
       response.send(output); 
      } 
     }); 

    } 

}); 
+0

如果请求期间发生错误,该怎么办? –

+0

@stdob:是的,好点,我会补充。此外,当request.body.UPRNS.length为零时,请将此提问。 – psiphi75

+0

太棒了,像魅力一样工作。将处理空请求。感谢您的帮助。 – noexpert