Import
Usage
Parameters
number
required
The timeout duration in milliseconds.
HTTPException | HTTPExceptionFunction
The exception to throw when the timeout occurs. Can be:
- An
HTTPExceptionobject - A function that receives the context and returns an
HTTPException - Default:
HTTPException(504, { message: 'Gateway Timeout' })
Signature
Examples
Basic usage
Custom error message
Dynamic error based on context
Different timeouts for different routes
With error handler
Timeout for specific async operations
Behavior
- Races the handler execution against a timeout timer
- Throws the specified HTTPException if timeout is reached
- Default exception is 504 Gateway Timeout
- Cleans up the timer after completion (whether success or timeout)
- Works with async handlers
- Timer is cleared even if handler throws an error
- Can be applied globally or to specific routes
- Multiple timeout middleware can be chained (innermost takes precedence)