2016-04-29 361 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