【发布时间】:2011-09-02 00:32:12
【问题描述】:
有一个帖子:How do I set a timeout for client http connections in node.js
但没有一个答案会奏效。
所以,我有这样的代码:
var remote_client = http.createClient(myPost, myHost);
var path = '/getData?';
var param = { };
var request = remote_client.request("POST", path,);
// error case
remote_client.addListener('error', function(connectionException){
console.log("Nucleus Error: " + connectionException);
next(connectionException);
});
request.addListener('response', function (response) {
response.setEncoding('utf-8');
var body = '';
response.addListener('data', function (chunk) {
// get the result!
});
});
request.end();
最大的问题是我连接的 url 可能会超时。因此,我想设置一个超时时间,比如 15 秒。如果是这样,触发一个监听器。
但是,我在 http.createClient 的文档中没有看到任何超时功能。请指教。谢谢。 :)
【问题讨论】:
-
在这个重复的问题中查看答案:stackoverflow.com/questions/6214902/…(尤其是看到 douwe 的答案)