Skip to content

Commit 27b4098

Browse files
authored
Merge branch 'main' into development
2 parents 242f3e2 + b9ec197 commit 27b4098

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

backend/app.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from "path";
2+
import { fileURLToPath } from "url";
13
import express from "express";
24
import dotenv from "dotenv";
35
// Import cors to allow requests from any origin
@@ -34,12 +36,28 @@ dotenv.config({
3436
connectDB();
3537
//#Initialize middleware
3638
const app = express();
37-
//#Also use the cors middleware for cross-origin requests.In this case, frontend requests!
38-
app.use(cors());
39-
40-
// Middleware to parse the body but only with tester like Postman or others
4139
app.use(express.json());
42-
// CORS middleware to allow requests from any origin eg. frontend!
40+
41+
//#Enable CORS to allow requests from frontend or other origins.
42+
const allowedOrigins = [
43+
process.env.FRONTEND_URL || "http://localhost:5173",
44+
process.env.FRONTEND_URL_PROD || "https://your-frontend.onrender.com",
45+
];
46+
47+
app.use(
48+
cors({
49+
origin: (origin, callback) => {
50+
if (!origin) return callback(null, true);
51+
if (allowedOrigins.includes(origin)) {
52+
callback(null, true);
53+
} else {
54+
console.error(`Blocked by CORS: ${origin}`);
55+
callback(new Error("Not allowed by CORS"));
56+
}
57+
},
58+
credentials: true,
59+
})
60+
);
4361

4462
//#Profile routes for user profile management
4563
app.use("/profile", userProfileRoutes);

frontend/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/src/components/utils/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { getStoredToken } from "./auth";
33

44
// Central API_BASE_URL for all API calls!
5-
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL;
5+
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "/api";
66
// Check if the environment variable is set
77
if (!API_BASE_URL) {
88
console.error(

frontend/vite.config.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
import { defineConfig } from "vite";
1+
import { defineConfig, loadEnv } from "vite";
22
import react from "@vitejs/plugin-react";
3+
import path from "path";
34

4-
// https://vite.dev/config/
5-
export default defineConfig({
6-
plugins: [react()],
5+
export default defineConfig(({ mode }) => {
6+
const env = loadEnv(mode, path.dirname(new URL(import.meta.url).pathname));
7+
console.log("VITE_RENDER_BACKEND_URL:", env.VITE_RENDER_BACKEND_URL);
8+
console.log("VITE_API_BASE_URL:", env.VITE_API_BASE_URL);
9+
console.log("VITE_FRONTEND_URL:", env.VITE_FRONTEND_URL);
10+
console.log("VITE_FRONTEND_URL_PROD:", env.VITE_FRONTEND_URL_PROD);
11+
console.log("NODE_ENV:", env.NODE_ENV);
12+
console.log("MONGO_URL:", env.MONGO_URL);
13+
console.log("Loaded environment variables for mode:", mode);
14+
console.log("Loaded environment", env);
15+
16+
return {
17+
plugins: [react()],
18+
server: {
19+
proxy: {
20+
"/api": {
21+
target: env.VITE_RENDER_BACKEND_URL || "http://localhost:5000",
22+
changeOrigin: true,
23+
rewrite: (path) => path.replace(/^\/api/, ""),
24+
},
25+
},
26+
},
27+
};
728
});

0 commit comments

Comments
 (0)