2017-06-04 138 views
0

我在節點休息api函數接收ajax調用id重新啓動url鏈接....它的作品,當我調用這個Ajax函數5次...我打電話後6它不工作..使用console.log我看到該行停止獲取請求,如果我再次調用它,我得到相同的日誌...從url(url有效)獲取請求...所以我看到請求模塊沒有返回,甚至任何錯誤或請求響應代碼...這裏是我的代碼:NodeJS請求模塊在6個Ajax請求後停止工作

apiRoutes.post('/restart_stream', function(req, res) { 
/* GET - token */ 
var token = req.headers['x-access-token']; 
    token = decrypt(token); 
    console.log('called.../restart_stream'); 

/* GET - data id */ 
var data  = {}; 
data.id  = req.body.id; 
data.type = req.body.type; 

    /* GET - connection */ 
    pool.getConnection(function(err,connection){ 
     if (err) { 
      connection.release(); 
      res.json({"code" : 100, "status" : "Error in connection database"}); 
      return; 
     } 

     /* GET - stream url */ 
     connection.query("SELECT id, channel, JSON_UNQUOTE(json_extract(stream, '$[0]')) AS stream_url FROM streams WHERE id=?", [data.id],function(err, rows, fields){ 
     console.log('getting request to...'+rows[0].stream_url); 
      /* GET - stream status */ 
      var r = request 
       .get(rows[0].stream_url) 
       .on('response', function(response) { 
        console.log('resoponse received....'+response.statusCode); 
        /* CHECK - stream exists */ 
        if (response.statusCode != 200 && response.statusCode != 302) { 
         r.end(); 
         /* RESET - live&streams info */ 
         res.json({"status": "false", "message": "FAILED to Restart <b>"+rows[0].channel+"</b>!"}); 
        } else { 
         /* RESET - live&streams info */ 
         connection.query("UPDATE streams SET uptime=?, status=? WHERE id=?",['0', '1', data.id], function(err) { 
          /* CHECK - error */ 
          if (err) { 
           res.json({"status": "false", "message": err.message}); 
          } else { 
           /* STOP - url my code here that is not executed after 6 ajax call */ 


           r.end(); 

           res.json({"status": "true", "message": "RESTARTED Successfully!"}); 
          }; 
         }); 
        }; 
       }) 
     }); 
    }); 
} else { 
    res.json({"status": "false", "message": "FAILED"}); 
}; 
}); 

我嘗試使用r.end(),以便請求連接,每個響應,但要求模塊停止後關閉5阿賈克斯後工作call..any線索我即時工作錯了?

這是一個連接到URL,並得到響應碼節點請求模塊:

https://www.npmjs.com/package/request 
+0

我不知道這是否是這裏的問題,但您不要在每個代碼路徑上調用'r.end',例如發生錯誤。順便說一句,這是一個「厄運金字塔」的完美例子,我會考慮重組該計劃。 –

+0

你有任何暗示或理論,我需要如何重組計劃? – John

+0

您並不總是釋放數據庫連接,因此在某些時候池中將沒有可用的連接,並且將開始等待連接被釋放。 – robertklep

回答

0

我是今天的成功和解決問題......問題是,我已經使用舊版本的節點的JS(V0。 10.25)現在升級到v7.10.0和所有工作我做了100個請求,並且工作得很好...我也有巨大的加載頁面,之前..所有節點js的新版本來與巨大的性能增加...