-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (56 loc) · 1.65 KB
/
Copy pathserver.js
File metadata and controls
61 lines (56 loc) · 1.65 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
60
61
import express from "express";
import { createProxyMiddleware } from "http-proxy-middleware";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = 3000;
const isDev = process.env.NODE_ENV !== "production";
const codeforgeUrl = process.env.CODEFORGE_URL || "http://localhost:8080";
// Backend serves /health and /metrics at root, but the UI calls /api/v1/health etc.
// Express strips the mount path before passing to proxy, so req.url is "/"
app.use(
"/api/v1/health",
createProxyMiddleware({
target: codeforgeUrl,
changeOrigin: true,
pathRewrite: { "^/": "/health" },
}),
);
app.use(
"/api/v1/metrics",
createProxyMiddleware({
target: codeforgeUrl,
changeOrigin: true,
pathRewrite: { "^/": "/metrics" },
}),
);
// API proxy — Express strips the mount path "/api", so we prepend it back
app.use(
"/api",
createProxyMiddleware({
target: codeforgeUrl,
changeOrigin: true,
pathRewrite: (p) => `/api${p}`,
}),
);
if (isDev) {
const { createServer } = await import("vite");
const vite = await createServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const { default: compression } = await import("compression");
app.use(compression());
app.use(express.static(path.resolve(__dirname, "dist")));
app.get("*", (_req, res) => {
res.sendFile(path.resolve(__dirname, "dist", "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(
`Server running in ${isDev ? "development" : "production"} mode on http://localhost:${PORT}`,
);
});