-
Notifications
You must be signed in to change notification settings - Fork 4
/
proxy.ts
39 lines (34 loc) · 1.12 KB
/
proxy.ts
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
import { createServer } from "http";
import { createProxyServer } from "http-proxy";
const proxy = createProxyServer();
proxy.on("error", (err) => console.warn(err));
const server = createServer((req, res) => {
const frontendPort = process.env.FRONTEND_PORT ?? 3001;
const backendPort = process.env.BACKEND_PORT ?? 3000;
req.url = req.url ?? "/";
if (
doesProxyContextMatchUrl(process.env.PROXY_PREFIX ?? "/open-api", req.url)
) {
req.url = req.url.replace(
RegExp(process.env.PROXY_PREFIX ?? "^/open-api"),
""
);
proxy.web(req, res, {
target: `http://localhost:${backendPort}`,
changeOrigin: true,
});
return;
}
proxy.web(req, res, { target: `http://localhost:${frontendPort}` });
});
server.on("error", (err) => console.warn(err));
console.log(
`Proxy start listening on port ${process.env.PROXY_LISTEN_PORT ?? 4000}`
);
server.listen(parseInt(process.env.PROXY_LISTEN_PORT ?? "4000"));
function doesProxyContextMatchUrl(context: string, url: string): boolean {
return (
(context.startsWith("^") && new RegExp(context).test(url)) ||
url.startsWith(context)
);
}