Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 502
Dima Grossman Avatar asked Oct 01 '15 08:10

Dima Grossman


People also ask

What is server timeout Nodejs?

Definition and Usage. The server.timeout property sets, or gets, the server's timeout value, in milliseconds. Default is 2 minutes (120000 milliseconds) Set the server. timeout property to 0 to avoid having a default timeout value.

What is the default timeout in Express?

By default, normal HTTP requests to Node. js/Express/Sails. js apps time out after 2 minutes (120000 milliseconds) if a response is not sent.

How do I increase my Express timeout?

Simply put, you need to configure the timeout value on the HTTP Server that express generates when you call the listen method. For example: // create an express app const app = express(); // add a route that delays response for 3 minutes app. get('/test', (req, res) => { setTimeout(() => { res.

CAN node js handle high traffic?

Since Node. js uses non-blocking IO, the server can handle multiple requests without waiting for each one to complete, which means Node. js can handle a much higher volume of web traffic than other more traditional languages.


2 Answers

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.

like image 150
Ionut Avatar answered Oct 18 '22 00:10

Ionut


how about:

server.on('connection', function(socket) {
  socket.setTimeout(5 * 60 * 1000);
  socket.once('timeout', function() {
    process.nextTick(socket.destroy);
  });
});
like image 2
num8er Avatar answered Oct 18 '22 01:10

num8er