Skip to content

504 Gateway Timeout

The middleman (proxy) gave up waiting for the upstream server to respond.

Use this on reverse proxies. If your API gateway forwards a request to a microservice, but that microservice takes 60 seconds to respond, the gateway should kill the request and return a 504.

import { HttpException, get } from 'shokupan';
export const proxyReport = get('/report', async () => {
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 10000); // 10s
const res = await fetch('http://slow-service.internal/report', {
signal: controller.signal
});
clearTimeout(timeout);
return res.json();
} catch (err) {
if (err.name === 'AbortError') {
throw new HttpException('Upstream service took too long', 504);
}
throw new HttpException('Internal Error', 500);
}
});