@@ -3,7 +3,7 @@ import userRoutes from "./routes/userRoutes.js";
33import monthlyLimitRoutes from "./routes/MonthlyLimit.route.js" ; // Import the new route
44import expenseRouter from "./routes/expense.router.js" ;
55import inputRouter from "./routes/Input.router.js" ;
6- import imageRouter from "./routes/ImageUpload.routes.js"
6+ import imageRouter from "./routes/ImageUpload.routes.js" ;
77import cors from "cors" ;
88import path from "path" ;
99import { fileURLToPath } from "url" ;
@@ -12,10 +12,38 @@ const app = express();
1212
1313app . use (
1414 cors ( {
15- origin : process . env . CORS_ORIGIN || "*" ,
15+ origin : function ( origin , callback ) {
16+ // Allow requests with no origin (like mobile apps or curl requests)
17+ if ( ! origin ) return callback ( null , true ) ;
18+
19+ // If CORS_ORIGIN is set, use it; otherwise allow all origins
20+ const allowedOrigins = process . env . CORS_ORIGIN
21+ ? process . env . CORS_ORIGIN . split ( "," )
22+ : [ "*" ] ;
23+
24+ if (
25+ allowedOrigins . includes ( "*" ) ||
26+ allowedOrigins . indexOf ( origin ) !== - 1
27+ ) {
28+ callback ( null , true ) ;
29+ } else {
30+ callback ( new Error ( "Not allowed by CORS" ) ) ;
31+ }
32+ } ,
1633 credentials : true ,
17- methods : [ "GET" , "POST" , "PUT" , "DELETE" , "PATCH" , "OPTIONS" ] ,
18- allowedHeaders : [ "Content-Type" , "Authorization" , "X-Requested-With" ]
34+ methods : [ "GET" , "POST" , "PUT" , "DELETE" , "PATCH" , "OPTIONS" , "HEAD" ] ,
35+ allowedHeaders : [
36+ "Content-Type" ,
37+ "Authorization" ,
38+ "X-Requested-With" ,
39+ "Accept" ,
40+ "Origin" ,
41+ "Access-Control-Request-Method" ,
42+ "Access-Control-Request-Headers" ,
43+ ] ,
44+ exposedHeaders : [ "Content-Length" , "X-Foo" , "X-Bar" ] ,
45+ preflightContinue : false ,
46+ optionsSuccessStatus : 200 ,
1947 } )
2048) ;
2149
@@ -25,6 +53,22 @@ const __dirname = path.dirname(__filename);
2553app . use ( express . json ( { limit : "16kb" } ) ) ;
2654app . use ( express . urlencoded ( { extended : true , limit : "16kb" } ) ) ;
2755app . use ( express . static ( "public" ) ) ;
56+
57+ // Handle preflight requests explicitly
58+ app . options ( "*" , ( req , res ) => {
59+ res . header ( "Access-Control-Allow-Origin" , req . headers . origin || "*" ) ;
60+ res . header (
61+ "Access-Control-Allow-Methods" ,
62+ "GET,POST,PUT,DELETE,PATCH,OPTIONS,HEAD"
63+ ) ;
64+ res . header (
65+ "Access-Control-Allow-Headers" ,
66+ "Content-Type,Authorization,X-Requested-With,Accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers"
67+ ) ;
68+ res . header ( "Access-Control-Allow-Credentials" , "true" ) ;
69+ res . sendStatus ( 200 ) ;
70+ } ) ;
71+
2872const buildPath = path . join ( __dirname , "../../FrontEnd/dist" ) ;
2973app . use ( express . static ( buildPath ) ) ;
3074
@@ -33,8 +77,7 @@ app.use("/api/addInput", inputRouter);
3377app . use ( "/api/expense" , expenseRouter ) ;
3478app . use ( "/api/auth" , userRoutes ) ;
3579app . use ( "/api/monthly" , monthlyLimitRoutes ) ;
36- app . use ( "/api/upload" , imageRouter )
37-
80+ app . use ( "/api/upload" , imageRouter ) ;
3881
3982app . get ( "/" , ( req , res ) => {
4083 res . send ( "Hello World" ) ;
0 commit comments