相关疑难解决方法(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万
查看次数

如何检测用户取消请求

我正在通过编写一个非常基本的 http/web 缓存代理来尝试 Node.js,并且遇到了一些我无法突破的问题。

假设我有一个非常基本的代理功能(侦听请求,将其传送到外部服务器,等待响应,将其传送回客户端),我如何检测客户端(Web 浏览器)何时取消请求?当用户在浏览器上单击“停止”/Esc 时,浏览器不会向我发送任何“请求”或信息,并且不会调用“响应”连接结束时的回调。

这就是我的意思:

http.createServer (clientRequest, clientResponse) {  
    var client = http.createClient (port, hostname);
    var request = client.request (method, url, headers);  

    request.addListener ('response', function(response){  
        response.addListener ('data', function(chunk){  
           // forward data to clientResponse..
        }  
        response.addListener ('end', function(){   
           // end clientResponse..  
        }  
    });  
    clientResponse.addListener('end', function(){  
        // this never gets called :(
        // I want it to terminate the request/response created just above  
    }
}
Run Code Online (Sandbox Code Playgroud)

proxy http node.js

5
推荐指数
1
解决办法
1908
查看次数

如何在Node.js中为http.createClient设置超时?

有一篇文章:如何在node.js中为客户端http连接设置超时

但没有一个答案会奏效.

所以,我有这样的代码:

    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();
Run Code Online (Sandbox Code Playgroud)

最大的问题是我连接的网址可能会超时.因此,我想设置一个超时,如15秒.如果是,则触发侦听器.

但是,我没有在http.createClient的文档中看到任何超时功能.请指教.谢谢.:)

timeout node.js

4
推荐指数
1
解决办法
5362
查看次数

标签 统计

node.js ×3

http ×2

timeout ×2

proxy ×1