javascript Nodejs 和 express 服务器在 2 分钟后关闭连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32882507/
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
Nodejs and express server closes connection after 2 minutes
提问by Dima Grossman
Im using Express 4.X and node js 0.12.
我使用 Express 4.X 和 node js 0.12。
One of my routes is for file uploading and processing and for some of the files the upload and process takes more than the 2 minutes default timeout. I have tried to settimeout to values more than 2 minutes but its just not working, the server closes connection exactly after 2 minutes every time.
我的路线之一是用于文件上传和处理,对于某些文件,上传和处理需要超过 2 分钟的默认超时时间。我试图将超时设置为超过 2 分钟的值,但它不起作用,服务器每次都在 2 分钟后关闭连接。
server.timeout = 60 * 60 * 1000; // still closes after 2 minutes
server.on('connection', function(socket) {
socket.setTimeout(700 * 1000); // still closes after 2 minutes
});
res.setTimeout(0);// still closes after 2 minutes
req.setTimeout(0);// still closes after 2 minutes
res.connection.setTimeout(0);// still closes after 2 minutes
The connect-timeout middleware is not helped either, it just keeps closing the connection after exactly 2 minutes. Tried changing the node version to older version but with no success. Tried all the variations found online, but the connection still closes...
连接超时中间件也没有帮助,它只是在 2 分钟后一直关闭连接。尝试将节点版本更改为旧版本,但没有成功。尝试了网上找到的所有变体,但连接仍然关闭...
采纳答案by Dima Grossman
After a few hours of trying every answer available I had run an inspection with fiddler for that request. Turns out that in my development environment im using browser-syncfor auto refreshing the browser window on any change. In fiddler i noticed that a long with the upload POST request browser-sync tied it to a socket connection which had 2 minute timeout.
在尝试了每个可用的答案几个小时后,我使用提琴手对该请求进行了检查。事实证明,在我的开发环境中,我使用浏览器同步在任何更改时自动刷新浏览器窗口。在 fiddler 中,我注意到上传 POST 请求浏览器同步很长一段时间将其绑定到一个套接字连接,该连接超时 2 分钟。
after switched off the browser-sync proxy the very first solution worked like a charm.
关闭浏览器同步代理后,第一个解决方案就像一个魅力。
server.on('connection', function(socket) {
socket.setTimeout(600 * 60 * 1000); // now works perfectly...
})
回答by Ionut Necula
server.setTimeout()is the method that sets the HTTP connection timeout for all connections.
server.setTimeout()是为所有连接设置 HTTP 连接超时的方法。
The 2 minutes are default.
2 分钟是默认值。
UPDATED ANSWER
更新的答案
Try this:
试试这个:
var express = require('express');
var http = require('http');
var app = module.exports.app = express();
var server = http.createServer(app);
server.setTimeout(10*60*1000); // 10 * 60 seconds * 1000 msecs
server.listen(appConfig.port, function () {
var logger = app.get('logger');
logger.info('**** STARTING SERVER ****');
});
Or this:
或这个:
http.request(url).setTimeout()
Also, it can be a browser issue. Read this.
此外,这可能是浏览器问题。读这个。
回答by num8er
how about:
怎么样:
server.on('connection', function(socket) {
socket.setTimeout(5 * 60 * 1000);
socket.once('timeout', function() {
process.nextTick(socket.destroy);
});
});