fix: load .env before imports and improve dev startup

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-27 22:22:19 +08:00
parent 8c56dbc459
commit d20a2639b6
9 changed files with 359 additions and 22 deletions

View File

@@ -1,12 +1,11 @@
import "./load-env.js";
import cors from "cors";
import dotenv from "dotenv";
import express from "express";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { MulterError } from "multer";
import { buildsRouter } from "./routes/builds.js";
dotenv.config({ path: path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.env") });
const app = express();
const port = Number(process.env.PORT ?? "3001");
@@ -19,6 +18,28 @@ app.get("/api/health", (_req, res) => {
app.use("/api/builds", buildsRouter);
app.use((err: unknown, _req: express.Request, res: express.Response, next: express.NextFunction) => {
if (res.headersSent) {
next(err);
return;
}
if (err instanceof MulterError) {
const status = err.code === "LIMIT_FILE_SIZE" ? 413 : 400;
res.status(status).json({ error: err.message });
return;
}
if (err instanceof Error) {
console.error(err);
res.status(500).json({ error: err.message });
return;
}
console.error(err);
res.status(500).json({ error: "Internal server error" });
});
const webDist = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../web/dist");
app.use(express.static(webDist));
app.get("*", (_req, res, next) => {
@@ -31,6 +52,6 @@ app.get("*", (_req, res, next) => {
});
});
app.listen(port, () => {
console.log(`Web2App server listening on http://localhost:${port}`);
app.listen(port, "127.0.0.1", () => {
console.log(`Web2App server listening on http://127.0.0.1:${port}`);
});