首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有requestify POST的Node.js超时

带有requestify POST的Node.js超时
EN

Stack Overflow用户
提问于 2016-06-30 17:05:24
回答 2查看 1.6K关注 0票数 1

我试图请求一个用户的状态,从node.js到一个PHP文件的帖子。我的问题是webservice调用的响应非常慢(4秒),所以我认为.then在4秒之前就完成了,因此什么也不返回。知道我能不能延长申请的时间?

代码语言:javascript
复制
requestify.post('https://example.com/', {
              email: 'foo@bar.com'
            })
            .then(function(response) {
                var answer = response.getBody();
                console.log("answer:" + answer);
            });
EN

回答 2

Stack Overflow用户

发布于 2016-07-07 21:43:44

我对requestify不是很了解,但是你确定你可以使用post到https地址吗?在自述文件中仅requestify.request(...)以https地址为例。(see readme)

不过,我可以肯定地给你一个小贴士,那就是永远抓住你的承诺:

代码语言:javascript
复制
requestify.get(URL).then(function(response) {
    console.log(response.getBody())
}).catch(function(err){
    console.log('Requestify Error', err);
    next(err);
});

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

票数 2
EN

Stack Overflow用户

发布于 2018-02-19 07:51:49

每次调用Requestify都允许您传递一个Options对象,该对象的定义如下所示:Requestify API Reference

您使用的是POST的short方法,所以我将首先说明这一点,但同样的语法也适用于put,请注意,getdeletehead不接受数据参数,您可以通过params配置属性发送url查询参数。

代码语言:javascript
复制
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秒的超时:

代码语言:javascript
复制
var config = {};
config.timeout = 60000;
requestify.post(url, data, config)

或内联:

代码语言:javascript
复制
requestify.post(url, data, { timeout: 60000 })

所以现在让我们把这些放到你的原始请求中:

正如@Jabalaja指出的那样,你应该捕获任何异常消息,但是你应该在继续中使用

参数来做这件事。(.then)

代码语言:javascript
复制
requestify.post('https://example.com/', {
    email: 'foo@bar.com'
}, {
    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);
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38118523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档