2015-11-15 88 views
0

我正在使用节点模块“请求”将一些JSON发送到我的REST API。节点 - 请求重试429错误?

我有下列呼叫

request({ 
    uri: urlToUse, // Using the url constructed above. 
    method: "POST", // POSTing to the URI 
    body: JSON.stringify(jsonItems), 
    headers: // Allows us to authenticate. 
    { 
     'Content-Type' : 'application/json', 
     'Authorization' : auth 
    }}, 
    //What to do after the request... 
    function(err, response, body) 
    { 
     // Was there an error? 
     if (err) 
     { 
      // If so, log it. 
      console.log(err); 
     } 
     // Did the server respond? 
     if (response) 
     { 
      // Log it. 
      console.log(response.statusCode); 

      // Did the response have a body? 
      if(body) 
      { 
       // Log it. 
       console.log(body); 
      } 
     } 
    }); 

我想要添加到这一点 - 我想能够作用于429个状态码 - 为了让它重试,直到完成请求。

我知道如何检测429(使用if语句来检查response.statusCode等),但是我不知道如何让它重试,或者甚至是如何做到最好。

+0

https://www.npmjs.com/package/request我想这是你所使用的模块然后? –

+0

是的,但我没有看到任何关于重试的信息。 :( – MickeyThreeSheds

回答

0

你想要做的是只是包装在一个函数所有代码,然后把它称自己如果响应代码为429。甚至包括一个“尝试尝试”号作为最后一个可选参数,在我看来,此功能,你就可以保持你有多少次把它称为一个计数,并采取相应的行动,以自己的喜好

相关问题