简体   繁体   English

Node.js超时并请求POST

[英]Node.js timeout with requestify POST

Im trying to request a status of a user, with a POST from node.js to a PHP file. 我试图通过从node.js到PHP文件的POST来请求用户的状态。 My issue is that the webservice Im calling is VERY slow to reply(4 sec), so I think the .then finishes before the 4 sec, and therefore returns nothing. 我的问题是,我呼叫的网络服务回复非常慢(4秒),所以我认为.then在4秒之前完成,因此什么都不返回。 Got any idea if i can extend the time for the request? 知道我是否可以延长请求的时间?

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

I am not that knowledgeable on requestify but are you sure you can use post to a https address? 我对请求知之甚少,但您确定可以使用帖子到https地址吗? In the readme only requestify.request(...) uses a https address as an example. 在自述文件中,只有requestify.request(...)使用https地址作为示例。 ( see readme ) 见自述

One tip I can definitely give you though is to always catch your promise: 我绝对可以给你的一个提示是始终抓住你的承诺:

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

This should at least give you the error of your promise and you can specify your problem. 这至少应该给你承诺的错误,你可以指定你的问题。

Each call to Requestify allows you to pass through an Options object, the definition of that object is described here: Requestify API Reference 每次调用Requestify都允许您传递一个Options对象,此处描述了该对象的定义: Requestify API Reference

You are using the short method for POST, so I'll show that first, but this same syntax will work for put as well, notice that get , delete , head do not accept a data argument, you send url query parameters through the params config property. 你正在使用short方法进行POST,所以我先说明一下,但同样的语法也适用于put ,注意getdeletehead不接受数据参数,你通过params发送url查询参数配置属性。

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

Now, config has a timeout property 现在, config具有timeout属性

timeout {number} 超时 {number}

Set a timeout (in milliseconds) for the request. 设置请求的超时(以毫秒为单位)。

So we can specify the a timeout of 60 seconds with this syntax: 因此我们可以使用以下语法指定60秒的超时:

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

or inline: 或内联:

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

So lets put that together now into your original request: 所以我们现在将它们放在原始请求中:

as @Jabalaja pointed out, you should catch any exception messages, however you should do this with the error argument on the continuation. 正如@Jabalaja指出的那样,你应该捕获任何异常消息,但是你应该使用continuation上的error参数来执行此操作。 ( .then ) .then

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM