Files
controller-v2/server/index.ts
T
2026-06-03 15:42:58 +08:00

49 lines
1.5 KiB
TypeScript

import express from "express";
import { createServer } from "http";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const DEPLOY_PREFIX = "/x9/v2";
async function startServer() {
const app = express();
const server = createServer(app);
const distRoot = path.resolve(__dirname, "..", "dist");
const modernPath = path.join(distRoot, "modern");
const legacyPath = path.join(distRoot, "legacy");
app.use(`${DEPLOY_PREFIX}/modern`, express.static(modernPath));
app.use(`${DEPLOY_PREFIX}/legacy`, express.static(legacyPath));
app.use(DEPLOY_PREFIX, express.static(distRoot));
const sendModernSpa = (_req: express.Request, res: express.Response) => {
res.sendFile(path.join(modernPath, "i.modern.html"));
};
const sendLegacySpa = (_req: express.Request, res: express.Response) => {
res.sendFile(path.join(legacyPath, "i.legacy.html"));
};
app.get(`${DEPLOY_PREFIX}/modern/*`, sendModernSpa);
app.get(`${DEPLOY_PREFIX}/legacy/*`, sendLegacySpa);
app.get([DEPLOY_PREFIX, `${DEPLOY_PREFIX}/`, `${DEPLOY_PREFIX}/i.html`], (_req, res) => {
res.sendFile(path.join(distRoot, "i.html"));
});
app.get("*", (_req, res) => {
res.redirect(302, `${DEPLOY_PREFIX}/i.html`);
});
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Server running on http://localhost:${port}${DEPLOY_PREFIX}/i.html`);
});
}
startServer().catch(console.error);