Reputation: 163
I have an upload system that worked just fine, but sever since I have implemented an SSL on my express server, any upload that takes longer than 2 minutes fails and I get net::ERR_FAILED
Where as previously any size file could take as long as it wanted and it got there eventually, but now 2 minutes exactly and it stops.
I have looked into a heap of posts that talk about the timeout for HTTPS in node.
And from those I have tried SO MANY different variations of timeout settings, but nothing seems to work.
Examples of things I have tried:
In my post route:
app.post(
'/uploadResources/', extendTimeout, uploadResources.array('file', 1), (req, res, next) => {
console.log("File must be done if you can read this...")
req.socket.setTimeout(500000);
res.json({
file: req.file,
});
req.end();
});
function extendTimeout (req, res, next) {
console.log("Started!");
// adjust the value for the timeout, here it's set to 3 minutes
res.setTimeout(500000, () => { console.log("timed out") })
next();
}
with these variations:
res.connection.setTimeout(0);
// infinite
res.connection.setTimeout(500000);
And where I define my server I have tried all these:
server.setTimeout(500000);
server.timeout = 500000;
server.on('connection', function(socket) {
socket.setTimeout(500000);
socket.setKeepAlive(true);
});
None of these made a difference! Anyway, before I added my SSL certs and was just using HTTP I defined my server like this:
var app = express();
// Listen on port 3000 for any calls
var server = app.listen(3000, function () {
console.log('VCS-API REST server started - yay.');
});
server.on('connection', function(socket) {
// 10 minutes timeout
socket.setTimeout(500000);
socket.setKeepAlive(true);
});
And this worked fine... however, I added my SSL certs and now I define the server like this (which works fine for smaller files that are done in less then 2 min):
const httpsOptions = {cert, ca, key};
const httpsServer = https.createServer(httpsOptions, app);
// Listen on port 443 for any calls
var server = httpsServer.listen(443, function () {
console.log('VCS-API REST server started - yay.');
});
server.on('connection', function(socket) {
// 10 minutes timeout
socket.setTimeout(500000);
socket.setKeepAlive(true);
});
So my exact question is: What is the correct way to set timeout in HTTPS to enable longer file uploads?
Upvotes: 3
Views: 2310
Reputation: 131
So I had some fun debugging this with op, it turns out there was a second connection being made using a package "multer-s3".
This is the connection that was having issues, not the file upload from browser->node.
To expose the issue an event listener for client error was added:
server.on('clientError', (err, socket) => {
console.log(err)
});
This showed a timeout error
ERR_HTTP_REQUEST_TIMEOUT
The unhandled error caused the whole connection to close. Simply handling the error in the event was enough for the request to complete as expected.
Without delving into the multer-s3 packaging there looks to be some kind of timeout issue when the underlying server is https. The fix for this was to catch the error, log it and allow the request to continue.
Upvotes: 3