2016-06-30 62 views
1

我试图请求一个用户的状态,从一个POST从node.js到一个PHP文件。 我的问题是,我的网络服务调用非常缓慢(4秒),所以我认为.then在4秒之前完成,因此不会返回任何内容。有任何想法,如果我可以延长请求的时间?Node.js超时与requestify POST

requestify.post('https://example.com/', { 
       email: '[email protected]' 
      }) 
      .then(function(response) { 
       var answer = response.getBody(); 
       console.log("answer:" + answer); 
      }); 

回答

2

我不是那种关于requestify的知识,但你确定你可以使用post到https地址吗?在自述文件中,只有requestify.request(...)使用https地址作为示例。 (see readme

一个提示,我绝对可以给你虽然是要始终抓住你的承诺:

requestify.get(URL).then(function(response) { 
 
    console.log(response.getBody()) 
 
}).catch(function(err){ 
 
    console.log('Requestify Error', err); 
 
    next(err); 
 
});

这至少应该给你承诺的错误,你可以指定你的问题。

1

到Requestify每次调用,您可以通过一个Options对象,该对象的定义描述如下:Requestify API Reference

您正在使用POST的short方法,所以我会表明,第一,但这个同样的语法也适用于put,请注意,get,delete,head不接受数据参数,您通过params config属性发送url查询参数。现在

requestify.post(url, data, config) 
requestify.put(url, data, config) 
requestify.get(url, config) 
requestify.delete(url, config) 
requestify.head(url, config) 

config具有timeout属性

超时为请求{数}

设置一个超时时间(毫秒)。

因此,我们可以用这种语法指定60秒的超时:

var config = {}; 
config.timeout = 60000; 
requestify.post(url, data, config) 

或内联:

requestify.post(url, data, { timeout: 60000 }) 

所以,现在让我们把它们一起到你的原始请求:

作为@Jabalaja指出,你应该捕获任何异常消息,howe你是否应该在延续中使用错误参数来做到这一点。 (.then

requestify.post('https://example.com/', { 
    email: '[email protected]' 
}, { 
    timeout: 60000 
}) 
.then(function(response) { 
    var answer = response.getBody(); 
    console.log("answer:" + answer); 
}, function(error) { 
    var errorMessage = "Post Failed"; 
    if(error.code && error.body) 
     errorMessage += " - " + error.code + ": " + error.body 
    console.log(errorMessage); 
    // dump the full object to see if you can formulate a better error message. 
    console.log(error); 
});