Repository navigation
Expand file tree
/
Copy pathmiddleware.js
More file actions
59 lines (51 loc) · 1.45 KB
/
Copy pathmiddleware.js
File metadata and controls
59 lines (51 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const HOP_BY_HOP = new Set([
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailer",
"transfer-encoding",
"upgrade",
"host"
]);
function resolveBackendOrigin() {
const origin = (process.env.RAILWAY_API_ORIGIN || process.env.VITE_SOCKET_URL || "").trim();
if (!origin || origin.startsWith("/")) {
return null;
}
return origin.replace(/\/$/, "");
}
export default async function middleware(request) {
const origin = resolveBackendOrigin();
if (!origin) {
return new Response(
JSON.stringify({ message: "Backend origin not configured. Set RAILWAY_API_ORIGIN on Vercel." }),
{ status: 500, headers: { "content-type": "application/json" } }
);
}
const incoming = new URL(request.url);
const target = `${origin}${incoming.pathname}${incoming.search}`;
const headers = new Headers(request.headers);
for (const key of HOP_BY_HOP) {
headers.delete(key);
}
const upstream = await fetch(target, {
method: request.method,
headers,
body: request.method === "GET" || request.method === "HEAD" ? undefined : request.body,
redirect: "manual"
});
const responseHeaders = new Headers(upstream.headers);
for (const key of HOP_BY_HOP) {
responseHeaders.delete(key);
}
return new Response(upstream.body, {
status: upstream.status,
statusText: upstream.statusText,
headers: responseHeaders
});
}
export const config = {
matcher: "/api/:path*"
};