我正在尝试在没有运气的情况下使用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)
任何提示?
我正在编写一个需要与服务器通信的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中这样做?