39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
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}...`);
|
||
});
|