Dima Grossman
Dima Grossman

Reputation: 2830

Nodejs and express server closes connection after 2 minutes

Im using Express 4.X and 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.

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...

Upvotes: 12

Views: 20563

Answers (3)

num8er
num8er

Reputation: 19372

how about:

server.on('connection', function(socket) {
  socket.setTimeout(5 * 60 * 1000);
  socket.once('timeout', function() {
    process.nextTick(socket.destroy);
  });
});

Upvotes: 2

Dima Grossman
Dima Grossman

Reputation: 2830

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-sync for 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.

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...
})

Upvotes: 1

Ionut Necula
Ionut Necula

Reputation: 11462

server.setTimeout() is the method that sets the HTTP connection timeout for all connections.

The 2 minutes are default.

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.

Upvotes: 16

Related Questions