HTTP request timed out before completing
Production Risk
Can affect legitimate users with slow connections; tune the timeout carefully.
Thrown when an incoming HTTP request body has not been fully received within the servers configured requestTimeout period. The server automatically destroys the socket and emits this error. It is primarily a server-side protection against slow-loris style attacks or very slow clients.
- 1Client is uploading a large body slower than the server timeout allows
- 2Network congestion causing the request body to arrive too slowly
- 3The server requestTimeout option is set too aggressively short
Triggered by the HTTP server internally when requestTimeout elapses before the full request is received.
const http = require('http');
const server = http.createServer({ requestTimeout: 1000 }, (req, res) => {
// slow client never finishes sending body
res.end('ok');
});
server.listen(3000);expected output
Error [ERR_HTTP_REQUEST_TIMEOUT]: Request timeout
Fix 1
Increase requestTimeout for endpoints with large uploads
WHEN When legitimate clients need more time to upload
const server = http.createServer(
{ requestTimeout: 30000 }, // 30 seconds
handler
);Why this works
A longer timeout accommodates slower clients while still protecting against indefinite hangs.
Fix 2
Handle the timeout event and return a 408 response
WHEN When you want to respond gracefully instead of dropping the connection
server.on('timeout', (socket) => {
socket.end('HTTP/1.1 408 Request Timeout\r\n\r\n');
});Why this works
Sending a proper 408 response before closing the socket gives the client a meaningful error code.
const http = require('http');
const server = http.createServer({ requestTimeout: 1000 }, (req, res) => {
// slow client never finishes sending body
res.end('ok');
});
server.listen(3000); // this triggers ERR_HTTP_REQUEST_TIMEOUTtry {
// operation that may throw ERR_HTTP_REQUEST_TIMEOUT
riskyOperation()
} catch (err) {
if (err.code === 'ERR_HTTP_REQUEST_TIMEOUT') {
console.error('ERR_HTTP_REQUEST_TIMEOUT:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_http_request_timeout(...args) {
// validate args here
return performOperation(...args)
}✕ Set requestTimeout to 0 (disabled) in public-facing servers
Disabling the timeout exposes the server to slow-loris DoS attacks.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev