Skip to content

Commit 42bbdd5

Browse files
committed
update build paths
1 parent 091246d commit 42bbdd5

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

client/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"scripts": {
77
"dev": "vite",
88
"start": "vite",
9-
"build": "vite build",
10-
"build:check-ts": "tsc && vite build",
9+
"build": "vite build --emptyOutDir",
10+
"build:check-ts": "tsc && vite build --emptyOutDir",
1111
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
1212
"preview": "vite preview"
1313
},
@@ -32,4 +32,4 @@
3232
"typescript": "^5.9.2",
3333
"vite": "^5.1.4"
3434
}
35-
}
35+
}

server/index.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import express, { Request, Response, NextFunction } from "express";
1+
import express from "express";
22
import cors from "cors";
33
import dotenv from "dotenv";
4-
import path from "path";
54
import router from "./routes.js";
6-
import { getVersion } from "./utils/getVersion.js";
7-
8-
const app = express();
9-
5+
import path from "path";
106
import { cleanReturnPayload } from "./utils/cleanReturnPayload.js";
117
import { fileURLToPath } from "url";
8+
129
dotenv.config({ path: "../.env" });
1310

1411
function checkEnvVariables() {
@@ -18,22 +15,42 @@ function checkEnvVariables() {
1815
if (missingVariables.length > 0) {
1916
throw new Error(`Missing required environment variables in the .env file: ${missingVariables.join(", ")}`);
2017
} else {
21-
console.info("All required environment variables provided.");
18+
console.log("All required environment variables provided.");
2219
}
2320
}
2421
checkEnvVariables();
2522

2623
const PORT = process.env.PORT || 3000;
24+
const app = express();
2725

2826
app.use(cors());
2927
app.use(express.json());
3028
app.use(express.urlencoded({ extended: false }));
29+
app.use("/api", router);
3130

32-
app.use(cors());
31+
if (process.env.NODE_ENV === "development") {
32+
const corsOptions = {
33+
origin: ["http://localhost:3000", "http://localhost:5173"],
34+
credentials: true, //access-control-allow-credentials:true
35+
optionSuccessStatus: 200,
36+
};
37+
app.use(cors(corsOptions));
38+
} else {
39+
// Node serves the files for the React app
40+
const __filename = fileURLToPath(import.meta.url);
41+
const __dirname = path.dirname(__filename);
42+
app.use(express.static(path.resolve(__dirname, "../../../client/build")));
43+
44+
// All other GET requests not handled before will return our React app
45+
app.get("*", (req, res) => {
46+
res.sendFile(path.resolve(__dirname, "../../../client/build", "index.html"));
47+
});
48+
}
3349

34-
app.use(function (req: Request, res: Response, next: NextFunction) {
50+
app.use(function (req, res, next) {
3551
const ogSend = res.send;
36-
res.send = function (data: any) {
52+
// @ts-ignore
53+
res.send = function (data) {
3754
if (data) {
3855
try {
3956
const cleanData = cleanReturnPayload(typeof data === "string" ? JSON.parse(data) : data);
@@ -44,24 +61,10 @@ app.use(function (req: Request, res: Response, next: NextFunction) {
4461
next();
4562
}
4663
}
47-
} as any;
64+
};
4865
next();
4966
});
5067

51-
app.use("/api", router);
52-
53-
if (process.env.NODE_ENV !== "development") {
54-
// Node serves the files for the React app
55-
const __filename = fileURLToPath(import.meta.url);
56-
const __dirname = path.dirname(__filename);
57-
app.use(express.static(path.resolve(__dirname, "../client/build")));
58-
59-
// All other GET requests not handled before will return our React app
60-
app.get("*", (req: Request, res: Response) => {
61-
res.sendFile(path.resolve(__dirname, "../client/build", "index.html"));
62-
});
63-
}
64-
6568
app.listen(PORT, () => {
66-
console.info(`Server is running on port: ${PORT}, version: ${getVersion()}`);
69+
console.log(`Server is running on port: ${PORT}`);
6770
});

0 commit comments

Comments
 (0)