Skip to content Skip to sidebar Skip to footer

Node.js Connection Reset On File Upload With Multer

I have a node/express app that handles file uploads with multer. Everything works well on my local machine, but on the server, if the uploaded file exceeds a couple of Mbs, the bro

Solution 1:

@NadavL was right, despite the fact that I had no front server, node was timing out itself.

It's written in the docs that there's no timeout in node by default. Express might override that but I couldn't find any information on the matter.

To define a specific timeout globally, you can proceed by changing the socket timeout when the server connects

[…]

// Start serverfunctionstartServer() {
    var port = 8888;
    server = app.listen(port, function () {
        console.log('Node version:' + process.versions.node);
        console.log('Express server listening on port %d in %s mode', port, app.settings.env);
    });

    server.on('connection', function(socket) {
        // 10 minutes timeout
        socket.setTimeout(10 * 60 * 1000);
    });
}

But as a high timeout rises your server's exposure to Slow HTTP Attacks, you might want to change the default timeout just for a specific route – in my case, just for the upload route. In this particular case, all you have to do is change the timeout in your route's handler like so:

app.post('/myroute', function (req, res) {
    // 10 minutes timeout just for POST to myroute
    req.socket.setTimeout(10 * 60 * 1000);
    upload.single('data');
    console.log(req.file);
});

There's also a dedicated middleware to handle timeouts, it's called connect-timeout and it can be used to configure specific timeouts for different routes too (see this post on SO).

Solution 2:

As far as I understood my issue, receiving ERR_CONNECTION_RESET after 60 seconds uploading big files. Increasing the request timeout with req.setTimeout(value, cb) isn't enough.

The server headersTimeout field needs to be increased manually as such:

server.headersTimeout = timeoutValue;

Node Doc on server_headers_timeout

Github issue describing the solution in details

Post a Comment for "Node.js Connection Reset On File Upload With Multer"