1
0
mirror of synced 2025-12-21 02:46:50 -05:00
Files
docs/middleware/timeout.js
Kevin Heis 8a56437c93 Pretty format (#20352)
* Update prettier flow to include JS

* Run prettier

* ...run prettier
2021-07-14 14:35:01 -07:00

28 lines
1001 B
JavaScript

import timeout from 'express-timeout-handler'
// Heroku router requests timeout after 30 seconds. We should stop them earlier!
const maxRequestTimeout = parseInt(process.env.REQUEST_TIMEOUT, 10) || 10000
export default timeout.handler({
// Default timeout for all endpoints
// To override for a given router/endpoint, use `xExpressTimeoutHandler.set(...)`
timeout: maxRequestTimeout,
// IMPORTANT:
// We cannot allow the middleware to disable the `res` object's methods like
// it does by default if we want to use `next` in the `onTimeout` handler!
disable: [],
onTimeout: function (req, res, next) {
// Create a custom timeout error
const timeoutError = new Error('Request timed out')
timeoutError.statusCode = 503
timeoutError.code = 'ETIMEDOUT'
// Pass the error to our Express error handler for consolidated processing
return next(timeoutError)
},
// Can also set an `onDelayedResponse` property IF AND ONLY IF you allow for disabling methods
})