function wrap(res, fn) {
    return function(...args) {
        if (res.timedout) {
            return res;
        } else {
            return fn.apply(res, args);
        }
    };
}

export default (timeoutValue) => {
    return (req, res, next) => {
        const {send, sendStatus, status} = res;
        res.setTimeout(timeoutValue);
        res.on('timeout', () => {
            res.timedout = true;
            if (!res.headersSent) {
                res.statusCode = 408;
                res.type('txt');
                send.apply(res, ['Request Timeout']);
            }
        });
        res.send = wrap(res, send);
        res.sendStatus = wrap(res, sendStatus);
        res.status = wrap(res, status);
        next();
    };
}
function wrap(res, fn) {
    return function(...args) {
        if (res.timedout) {
            return res;
        } else {
            return fn.apply(res, args);
        }
    };
}

export default (timeoutValue) => {
    return (req, res, next) => {
        const {send, sendStatus, status} = res;
        res.setTimeout(timeoutValue);
        res.on('timeout', () => {
            res.timedout = true;
            if (!res.headersSent) {
                res.statusCode = 503;
                res.type('txt');
                send.apply(res, ['Request Timeout']);
            }
        });
        res.send = wrap(res, send);
        res.sendStatus = wrap(res, sendStatus);
        res.status = wrap(res, status);
        next();
    };
}

Change from 408 to 503 since it is a server error
Change from 408 to 503 since it is a server error
