1- import express , { Request , Response , NextFunction } from "express" ;
1+ import express from "express" ;
22import cors from "cors" ;
33import dotenv from "dotenv" ;
4- import path from "path" ;
54import router from "./routes.js" ;
6- import { getVersion } from "./utils/getVersion.js" ;
7-
8- const app = express ( ) ;
9-
5+ import path from "path" ;
106import { cleanReturnPayload } from "./utils/cleanReturnPayload.js" ;
117import { fileURLToPath } from "url" ;
8+
129dotenv . config ( { path : "../.env" } ) ;
1310
1411function 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}
2421checkEnvVariables ( ) ;
2522
2623const PORT = process . env . PORT || 3000 ;
24+ const app = express ( ) ;
2725
2826app . use ( cors ( ) ) ;
2927app . use ( express . json ( ) ) ;
3028app . 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-
6568app . 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