Documentation
Docs
Introduction
Getting Started
Tutorial: build an app
Core Concepts
Routing
Server-Side Rendering
Mimir, state management
Pages & Layouts
API Routes
Styling & Theming
Building & Deploying
API Reference
Guides
Recipes
FAQ (use cases)
timeout(ms)
Guarantee a request gets an answer: if the handler does not respond within ms, reply 504.
Use case
A slow dependency (a stuck database query, an unresponsive upstream) can leave a request hanging forever,
tying up the client and consuming server resources. A response deadline turns "hangs indefinitely" into a
predictable 504 Gateway Timeout, so clients can retry or fail fast.
How it works
It starts a timer when the request enters. It wraps res.json, res.text, and res.html so that sending a
response clears the timer. If the timer fires first (no response within ms), it replies
504 Gateway Timeout. The argument is the deadline in milliseconds.
Configuration
The single argument is the timeout in milliseconds.
| Argument | Example | Notes |
|---|---|---|
ms | 10000 | Deadline in ms before a 504 is sent. |
Example
Pair a generous global timeout with a tighter per-route one for endpoints you know should be fast.
Notes
A timeout protects the client's experience; it does not cancel the slow work already running server-side. Use it together with sensible timeouts on your own outbound calls (database, fetch) so the underlying operation is bounded too, not just the response.
Next: errorHandler.