【问题标题】:node http.get not fired节点 http.get 未触发
【发布时间】:2013-07-03 20:40:48
【问题描述】:

我尝试使用 http.get 请求进行循环 - 我不知道为什么该函数没有启动。 我的代码是:

while(List.length>0) {                
   if(counter < Limit) { // Limit is the amount of requests I want to 
        counter++;
        inProcess++;
        var scanitem =  List.pop();

      var body = "";    
      var url = 'https://www.mywebsite.com/'+scanitem;
      var options = {
                      path: url,                                                
                      method: 'GET'
                    };
      console.log(url); // This part is happenning
      var _request =  https.get(options, function (res) {
                       console.log('get web site');// this part is NOT showup. 
                       res.on('data', function (chunk) {
                          console.log(chunk); // this part is NOT showup. 
                          body += chunk.toString();            
                      });
                      res.on("end", function() {                            
                        console.log(body);// this part is NOT showup. 
                      });
                      res.on("error", function(error) {                         
                       console.log('error')// this part is NOT showup. 
                      });
                  });
       _request.end();         
   }                                        
   else {
      console.log('list BREAK');} // This part is happenning after the limit crossed

【问题讨论】:

  • 您可以先添加_request.on("error", function(error) 找出任何错误。因为如果在发送任何响应之前请求本身可能失败,则不会出现响应错误。
  • https 请求可能需要密钥或证书。由于您没有给任何服务器可能会拒绝请求本身。

标签: node.js httprequest


【解决方案1】:
  1. 当传递 Object 作为第一个参数时,URL 应该被分解成单独的部分:

    var options = {
        method: 'GET',
        protocol: 'https:',
        hostname: 'www.mywebsite.com',
        path: '/' + scanitem
    };
    
    var _request = https.get(options, ...);
    

    使用的options 包含在https.request() 下,https.get() 是其方便的变体。

    您还可以传递 URL String,https.get() 将通过 url.parse() 为您运行:

    var _request = https.get(url, ...);
    
  2. JavaScript 没有块范围的变量 (yet)。因此,尽管var body = ""; 的位置,您的while 循环的每次迭代仍然附加到相同的body。

    当变量仅由同步任务使用时,这不是一个问题,例如scanitem、url 和options。但是,当混合像 https.get() 这样的异步任务时,您可能不会得到您期望的结果。

    在当前没有块作用域变量的情况下,您可以使用closure 来创建额外的function 作用域。

    由于List 似乎是Array,您可以为此使用迭代器function 和.forEach():

    List.forEach(function (scanitem) {
        var body = '';
        var url = 'https://www.mywebsite.com/'+scanitem;
    
        https.get(url, function (res) {
            // etc.
        });
    });
    

    而且,对于Limit,您可以使用.splice() 删除并使用您想要的Array 部分:

    List.splice(0, Limit).forEach(function (scanitem) {
        // etc.
    });
    

另外,与https.request() 不同的是,使用https.get() 时不需要调用_request.end()。

【讨论】:

  • 仍然不起作用,当我将它从循环中取出时,它工作正常,但是当它是进程的一部分时 - 它以某种方式不启动请求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
  • 2015-12-22
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多