如何在 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();
有什么提示吗?
I'm trying to set a timeout on an HTTP client that uses http.request with no luck. So far what I did is this:
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();
Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在这里详细说明答案@douwe,您可以在 http 请求上设置超时。
this.abort() 也可以。
Elaborating on the answer @douwe here is where you would put a timeout on a http request.
this.abort() is also fine.
对我来说 - 这是一种不太令人困惑的方法
socket.setTimeout
For me - here is a less confusing way of doing the
socket.setTimeout
您应该将引用传递给请求,如下所示
请求错误事件将被触发
You should pass the reference to request like below
Request error event will get triggered
很好奇,如果直接使用
net.sockets
会发生什么?以下是我出于测试目的而整理的一些示例代码:Curious, what happens if you use straight
net.sockets
instead? Here's some sample code I put together for testing purposes:2019 更新
现在有多种方法可以更优雅地处理这个问题。请参阅此线程上的一些其他答案。技术发展很快,因此答案往往很快就会过时。我的答案仍然有效,但也值得考虑替代方案。
2012 答案
使用您的代码,问题是您在尝试在套接字对象上设置内容之前没有等待将套接字分配给请求。这都是异步的:
当请求被分配一个套接字对象时,会触发“socket”事件。
2019 Update
There are various ways to handle this more elegantly now. Please see some other answers on this thread. Tech moves fast so answers can often become out of date fairly quickly. My answer will still work but it's worth looking at alternatives as well.
2012 Answer
Using your code, the issue is that you haven't waited for a socket to be assigned to the request before attempting to set stuff on the socket object. It's all async so:
The 'socket' event is fired when the request is assigned a socket object.
只是为了澄清上面的答案:
现在可以使用
timeout
选项和相应的请求事件:请参阅文档:
Just to clarify the answer above:
Now it is possible to use
timeout
option and the corresponding request event:See the docs:
此时有一个方法可以直接在请求对象上执行此操作:
这是绑定到套接字事件然后创建超时的快捷方法。
参考: Node.js v0.8.8 手册&文档
At this moment there is a method to do this directly on the request object:
This is a shortcut method that binds to the socket event and then creates the timeout.
Reference: Node.js v0.8.8 Manual & Documentation
Rob Evans anwser 对我来说工作正常,但是当我使用 request.abort() 时,它会抛出一个未处理的套接字挂起错误。
我必须为请求对象添加一个错误处理程序:
The Rob Evans anwser works correctly for me but when I use request.abort(), it occurs to throw a socket hang up error which stays unhandled.
I had to add an error handler for the request object :
还有更简单的方法。
不使用 setTimeout 或直接使用套接字,
我们可以在客户端使用的“选项”中使用“超时”。
下面是服务器和客户端的代码,分为3部分。
模块和选项部分:
服务器部分:
客户端部分:
如果将以上 3 部分全部放在一个文件“a.js”中,然后运行:
那么,输出将是:
希望有帮助。
There is simpler method.
Instead of using setTimeout or working with socket directly,
We can use 'timeout' in the 'options' in client uses
Below is code of both server and client, in 3 parts.
Module and options part:
Server part:
Client part:
If you put all the above 3 parts in one file, "a.js", and then run:
then, output will be:
Hope that helps.