Skip to content
Source
Sign inSign up
samwillis/neon-glow-demo
Open History to see the last commit

proxy.server.ts

13 lines1.0 KB

1export async function forward({ request }: { request: Request }) {
2 const origin = process.env.GLOW_API_URL;
3 if (!origin) return Response.json({ error: 'The App API is not configured' }, { status: 503 });
4 const incoming = new URL(request.url), target = new URL(origin);
5 target.pathname = incoming.pathname; target.search = incoming.search;
6 const headers = new Headers(request.headers);
7 for (const name of ['host', 'content-length', 'connection']) headers.delete(name);
8 const response = await fetch(target, { method: request.method, headers, body: ['GET', 'HEAD'].includes(request.method) ? undefined : await request.arrayBuffer(), redirect: 'manual' });
9 // fetch has already decoded the body, so the upstream encoding and length no longer apply.
10 const output = new Headers(response.headers);
11 for (const name of ['content-encoding', 'content-length', 'transfer-encoding', 'connection']) output.delete(name);
12 return new Response(response.body, { status: response.status, statusText: response.statusText, headers: output });