Skip to content

Commit af31c57

Browse files
committed
Refactor CORS configuration to handle origins dynamically and improve preflight request handling
1 parent 44e4fe5 commit af31c57

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

BackEnd/src/app.js

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import userRoutes from "./routes/userRoutes.js";
33
import monthlyLimitRoutes from "./routes/MonthlyLimit.route.js"; // Import the new route
44
import expenseRouter from "./routes/expense.router.js";
55
import inputRouter from "./routes/Input.router.js";
6-
import imageRouter from "./routes/ImageUpload.routes.js"
6+
import imageRouter from "./routes/ImageUpload.routes.js";
77
import cors from "cors";
88
import path from "path";
99
import { fileURLToPath } from "url";
@@ -12,10 +12,38 @@ const app = express();
1212

1313
app.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);
2553
app.use(express.json({ limit: "16kb" }));
2654
app.use(express.urlencoded({ extended: true, limit: "16kb" }));
2755
app.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+
2872
const buildPath = path.join(__dirname, "../../FrontEnd/dist");
2973
app.use(express.static(buildPath));
3074

@@ -33,8 +77,7 @@ app.use("/api/addInput", inputRouter);
3377
app.use("/api/expense", expenseRouter);
3478
app.use("/api/auth", userRoutes);
3579
app.use("/api/monthly", monthlyLimitRoutes);
36-
app.use("/api/upload",imageRouter)
37-
80+
app.use("/api/upload", imageRouter);
3881

3982
app.get("/", (req, res) => {
4083
res.send("Hello World");

0 commit comments

Comments
 (0)