express-timeout-handler
raw JSON → 2.2.2 verified Sat Apr 25 auth: no javascript maintenance
Express middleware that enforces response timeouts by disabling response methods and calling a custom onTimeout handler. Current stable version is 2.2.2, with low release cadence (last major 2.0.0 ~2017). Unlike connect-timeout or other timeout middlewares, it actively prevents double-send by disabling response methods after timeout and supports per-route timeouts via timeout.set(). Requires Node >=6 and Express 4.x.
Common errors
error TypeError: timeout.handler is not a function ↓
cause Using import ES module syntax on a CommonJS package; or incorrect import path.
fix
Use require('express-timeout-handler').
error TimeoutError: The response object was ended after the request timed out ↓
cause onTimeout did not send a response, or another middleware called res.send after timeout.
fix
Ensure onTimeout calls res.end() or similar; check disable array if too restrictive.
error Cannot set property 'globalTimeout' of undefined ↓
cause timeout.handler(options) was attached before Express app initialization or on non-request object.
fix
Call app.use(timeout.handler(options)) after express() and before routes.
Warnings
gotcha onTimeout callback MUST terminate the request (call res.end/send/json) otherwise the request hangs indefinitely. ↓
fix Always call res.status().send() or similar in onTimeout.
gotcha Streaming responses are not interrupted by timeout handler; if a response stream started before timeout, onTimeout will not fire. ↓
fix Implement manual timeout handling inside stream if needed.
gotcha Disabling methods like 'write' and 'end' can break middleware that attempts to write response after timeout. ↓
fix Set disable array carefully; consider omitting it to use defaults.
breaking In v2.0.0, the signature changed: options object now expects 'onTimeout' instead of 'timeoutHandler' and 'disable' replaced 'protectedMethods'. ↓
fix Update options to use new property names.
deprecated The package is no longer actively maintained; last update 2019. ↓
fix Consider alternatives like 'express-timeout' or custom middleware.
Install
npm install express-timeout-handler yarn add express-timeout-handler pnpm add express-timeout-handler Imports
- timeoutHandler wrong
import timeout from 'express-timeout-handler';correctconst timeout = require('express-timeout-handler'); - handler wrong
app.use(timeout(options));correctapp.use(timeout.handler(options)); - set wrong
app.use(timeout.set(4000));correcttimeout.set(4000);
Quickstart
const express = require('express');
const timeout = require('express-timeout-handler');
const app = express();
const options = {
timeout: 5000,
onTimeout: function(req, res) {
res.status(503).send('Service unavailable. Please retry.');
},
onDelayedResponse: function(req, method, args, requestTime) {
console.log('Delayed response after timeout:', method);
},
disable: ['write', 'send', 'json', 'end']
};
app.use(timeout.handler(options));
app.get('/greet', function (req, res) {
setTimeout(() => res.send('Hello'), 6000); // Will timeout
});
app.get('/leave', timeout.set(2000), function (req, res) {
setTimeout(() => res.send('Goodbye'), 1000);
});
app.listen(3000, () => console.log('running'));