@rtorcato/api-timeout-express
v0.1.0
Published
Express middleware for @rtorcato/api-timeout — fail a request with 503 if it isn't answered within a deadline.
Maintainers
Readme
@rtorcato/api-timeout-express
Express middleware for @rtorcato/api-timeout.
Fails a request with 503 if the response isn't sent within a deadline.
import express from 'express'
import { timeoutMiddleware } from '@rtorcato/api-timeout-express'
const app = express()
// Any request not answered within 5s gets a 503 error envelope.
app.use(timeoutMiddleware({ ms: 5000 }))On timeout the client receives the standard error envelope from
@rtorcato/api-errors:
{ "error": "ServiceUnavailableError", "code": "service_unavailable", "message": "Request timed out" }The middleware sends the 503 and stops. A slow handler that later tries to write will hit Express's "headers already sent" — guard late writes with
res.headersSentin long-running handlers.
express is a peer dependency (v4 or v5) — you control the version.
License
MIT
