2016-01-13 35 views
2

我对我的节点api发出的请求需要超过4分钟的时间来响应。收到回复的时间。角度应用程序不接受响应。在萤火虫上,网址变成红色。节点应用程序中的请求超时

我该如何克服这一点。

api.route('/allsearch') 
    .post(function(req,res){ 
     var filters=req.body.everything; 
     var filterid=req.body.filterId; 
     var searchid=req.body.searchid; 
     var zipgroup=req.body.zipgroup; 
     var myObject = new Array(); 


     function getData(docs, filters, filterid, callback) { 
      function loop(i) { 
       searchingalgo(docs[i], filters, filterid, function(pers){ 
         myObject[i] = pers; 
         if (i < docs.length) { 
         loop(i + 1); 
         } else { 
          callback(); 
         } 
        }); 
      }; 
      loop(0); 
     };//closing get data() 

     Searchradius.findOne({"searchid" : searchid, user: req.decoded.id}).exec(function(err, docs) { 


      // Array to hold async tasks 
      var asyncTasks = []; 

      // Loop through some items 
      zipgroup.forEach(function(item){ 
       // We don't actually execute the async action here 
       // We add a function containing it to an array of "tasks" 
       asyncTasks.push(function(callback){ 
       // Call an async function, often a save() to DB 
       console.log(item); 
       searchingalgo(item, filters, filterid, function(pers){ 
         myObject[item] = pers; 
         // Async call is done, alert via callback 
        callback(); 
       }); 
       }); 
      }); 


      Async.parallel(asyncTasks, function(){ 
       //console.log(myObject); 
       Searchradius.update({ _id: searchid }, { $set: { ucounteds: myObject , uzips: zipgroup }}, function(err, result){ 
        if(err) { 
         res.send(err); 
         return; 
        } 
        var fields = ['city', 'state', 'zip','distance', 'count']; 
        var myresults = []; 
        var tc=0; 
        var newMyobj= new Array(); 
        co=0; 
        zipgroup.forEach(function(item){ 
         tc+=myObject[item]; 
         //myresults.push(jobj); 
        }); 

        for(i=0;i<zipgroup.length;i++){ 
         newMyobj[i]=myObject[zipgroup[i]]; 
        } 
        console.log(tc); 

        Searchfilter.update({ _id: filterid }, { $set: { counted_results: tc }}, function(err, resultupdate){ 
         //console.log(resultupdate); 
         //console.log(tc); 
        }); 




       // console.log(myObject); 
       // console.log(newMyobj); 
        res.json({ 
        success: true, 
        zips: zipgroup, 
        states: docs.states, 
        cities: docs.cities, 
        distances: docs.distances, 
        counted_results : newMyobj, 
        }); 

       });  //update searchradius 
      });   //getdata function 
     });    //searchradius findone 
    }); 

根据要求,这是我的节点API。 zipgroup是一组拉链,如

[37663, 37664, 37669, 37671, 37660, 37669, 37667, 37668, 37666, 37665, 37662, 37661] 

只是要清楚集合Consumer1s有2900009876个文档。它被正确编入索引,查询花费的时间最少。但我仍然面临这个问题。 任何建议都会有帮助。

这是从角控制器我的发布请求。

$http.post('/api/allsearch', 
     { 
      "everything":$scope.filterSearch , 
      "searchid":$routeParams.id, 
      "filterId": $scope.filterId, 
      "zipgroup" : $scope.zipgroup 
     }) 
     .success(function(data){ 

      for(var i=0; i<data.zips.length;i++){ 
       oneset={ 
        "zip": data.zips[i], 
        "state": data.states[i], 
        "city": data.cities[i], 
        "distance": data.distances[i], 
        "count": data.counted_results[i] 
       }; 
       $scope.totalCount+=data.counted_results[i]; 
       $scope.results.push(oneset); 
      } 
      angular.forEach($scope.results, function (result) { 
        result.distance = parseFloat(result.distance); 
       }); 
      $rootScope.processing=false; 
      $scope.filterlinkdisplay=true; 
    }); 
+0

我们看一些代码。 –

+0

@Satyam你需要获得任何帮助,如错误消息之前添加更多的细节,你的代码等 – sachinjain024

+0

添加一些代码...让我知道是否需要任何其他输入 –

回答

1

至少有几种选择:

  1. 设置AngularJS $http timeout 10分钟左右,让AngularJS请求不超时,等待4分钟,以获得数据
  2. 轮询:1)AngularJS应用目前初始请求2)Node.js加载服务器发出一个唯一的令牌AngularJS应用此请求,并开始于收集3)AngularJS应用数据等待几分钟的工作并执行的请求与先前RECE获取数据的令牌4)如果结果已准备就绪,您就完成了。如果不是,再次等待和AngularJS
  3. use WebSocket做的另一个请求。在客户端,它受到许多浏览器的支持,在服务器端使用ws。这是一个双向协议,因此服务器可以在数据准备就绪时通知客户端。
+1

感谢您的答案...我如何实现AngularJS $ http超时10分钟? –

+0

$ http.post( '/ API/allsearch', \t \t \t { \t \t \t \t “一切”:$ scope.filterSearch, \t \t \t \t “searchid”:$ routeParams。ID, \t \t \t \t “filterId”:$ scope.filterId, \t \t \t \t “zipgroup”:$ scope.zipgroup \t \t \t}) \t \t \t .success(功能(数据){ \t \t \t \t \t \t \t \t angular.forEach($ scope.results,功能(结果){ \t \t \t \t \t result.distance = parseFloat(result.distance); \t \t \t \t \t}); \t \t \t \t $ rootScope.processing = false; \t \t \t \t $ scope.filterlinkdisplay = true; \t \t}); –

+0

我从我的角度控制器添加了POST请求..请指教如何设置超时10分钟。 –