* Add middleware to timeout requests after a period * Add halt-on-dropped-connection middleware to stop the middleware processing stack if the connection was already dropped * Add a few strategic bail-out spots for dropped connections during the render-page middleware * Handle 404s and HEAD requests earlier in the page rendering flow * Add a few more strategic bail-out spots for dropped connections during the render-page middleware * Add middleware to notice aborted requests * Add a check for aborted requests into the isConnectionDropped logic * Reformat comment for consistency * Handle aborted requests correctly in the error handler * Explicit returns for consistency
19 lines
631 B
JavaScript
19 lines
631 B
JavaScript
function isConnectionDropped (req, res) {
|
|
// Have the flags been set for:
|
|
// - a global request timeout (via the express-timeout-handler middleware)?
|
|
// - an aborted request connection (via Node.js core's HTTP IncomingMessage)?
|
|
return Boolean(res.globalTimeout || req.aborted)
|
|
}
|
|
|
|
function haltOnDroppedConnection (req, res, next) {
|
|
// Only proceed if the flag has not been set for the express-timeout-handler middleware
|
|
if (!isConnectionDropped(req, res)) {
|
|
return next()
|
|
}
|
|
}
|
|
|
|
// Export this logic, too
|
|
haltOnDroppedConnection.isConnectionDropped = isConnectionDropped
|
|
|
|
module.exports = haltOnDroppedConnection
|