2016-01-13 34 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分鐘。 –