相关疑难解决方法(0)

如何在Node中的http.request()上设置超时?

我正在尝试在没有运气的情况下使用http.request的HTTP客户端设置超时.到目前为止我所做的是这样的:

var options = { ... }
var req = http.request(options, function(res) {
  // Usual stuff: on(data), on(end), chunks, etc...
}

/* This does not work TOO MUCH... sometimes the socket is not ready (undefined) expecially on rapid sequences of requests */
req.socket.setTimeout(myTimeout);  
req.socket.on('timeout', function() {
  req.abort();
});

req.write('something');
req.end();
Run Code Online (Sandbox Code Playgroud)

任何提示?

timeout http node.js

82
推荐指数
5
解决办法
11万
查看次数

如何在node.js中为客户端http连接设置超时

我正在编写一个需要与服务器通信的node.js应用程序.它使用以下代码建立http连接:

var client = http.createClient(u.port, u.hostname, u.secure);
client.on("error", function(exception) {
    logger.error("error from client");
});
var request = client.request(method, u.path, headers);
Run Code Online (Sandbox Code Playgroud)

我没有在node.js文档中看到任何用于在连接上设置超时的选项,默认情况下它似乎设置为20秒.我遇到的问题是我在中国的用户似乎是一个缓慢或片状的网络,他们有时会遇到连接到美国数据中心的超时.我想将超时时间增加到1分钟,看看是否能为它们修复它.

有没有办法在node.js中这样做?

node.js

10
推荐指数
2
解决办法
2万
查看次数

标签 统计

node.js ×2

http ×1

timeout ×1