ibms_ems/server.js
2026-06-11 15:36:50 -04:00

39 lines
1.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require("express");
const path = require("path");
const { PORT } = require("./config.js");
const app = express();
if (process.env.NODE_ENV === "production") {
// 🔧 URL rewrite 必須在所有路由之前執行(對應 Vite dev proxy 的行為)
app.use((req, res, next) => {
if (req.path.startsWith("/forge/api")) {
req.url = req.url.replace(/^\/forge\/api/, "/api");
req.path = req.path.replace(/^\/forge\/api/, "/api");
}
next();
});
const distPath = path.join(__dirname, "wwwroot/ibms_ems/dist");
app.use(express.static(distPath));
}
// 👉 API routes在 rewrite 之後)
app.use(require("./routes/auth.js"));
if (process.env.NODE_ENV === "production") {
// Vue Router history mode (Express 5 catch-all syntax)
app.get("/{*path}", (req, res) => {
res.sendFile(path.join(__dirname, "wwwroot/ibms_ems/dist", "index.html"));
});
} else {
// 👉 Dev只跑 API不管前端
app.get("/", (req, res) => {
res.send("API server running...");
});
}
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}...`);
});