Node.js 中是否有 http.request 的默认超时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23874954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a default timeout in Node.js for http.request?
提问by mdunisch
In Node.js there is a default timeout for a server(for an incoming HTTP request) at 120000ms (2 minutes) (see HTTP's server.timeoutdocumentation).
在 Node.js 中,服务器(对于传入的 HTTP 请求)的默认超时为120000 毫秒(2 分钟)(请参阅HTTP 的server.timeout文档)。
But if I want to do an HTTP request in Node.js (using http.request), looking at the documentation, I only find a function request.setTimeout()to set the timeout manually.
但是,如果我想在 Node.js (using http.request) 中执行 HTTP 请求,查看文档,我只能找到一个函数request.setTimeout()来手动设置超时。
Anyone know if there is a default timeout for HTTP requests in Node.js? Or does Node.js try to send the HTTP request with no end?
任何人都知道 Node.js 中的 HTTP 请求是否有默认超时?或者 Node.js 是否尝试无休止地发送 HTTP 请求?
回答by SomeKittens
You want to set the server.timeoutproperty (it defaults to 120,000, as you've found).
您想设置该server.timeout属性(如您所见,它默认为 120,000)。
回答by beatak
I was also interested in this. By reading the code, Node.js uses Socket under the hood of http request (naturally). (The source link below is referring v8.8.0 at the point when I'm writing this)
我也对这个很感兴趣。通过阅读代码,Node.js 在 http 请求的引擎盖下(自然地)使用了 Socket。(下面的源链接在我写这篇文章时指的是 v8.8.0)
https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js
https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js
And Socket does not have the timeout by default by this document
并且Socket默认没有超时这个文档
https://nodejs.org/dist/latest-v6.x/docs/api/net.html#net_socket_settimeout_timeout_callback
https://nodejs.org/dist/latest-v6.x/docs/api/net.html#net_socket_settimeout_timeout_callback
And the source tells the same.
消息来源也是如此。
回答by 131
No. There is no default timeout.
不。没有默认超时。
Node.js uses Socket under the hood of http request and socket does not have the timeout by default.
Node.js 在 http 请求的掩护下使用 Socket,默认情况下 socket 没有超时。
Use the {timeout: XX} parameter of http.request to configure a proper request timeout.
使用 http.request 的 {timeout: XX} 参数来配置适当的请求超时。

