File tree Expand file tree Collapse file tree 4 files changed +52
-13
lines changed
Expand file tree Collapse file tree 4 files changed +52
-13
lines changed Original file line number Diff line number Diff line change 1+ import path from "path" ;
2+ import { fileURLToPath } from "url" ;
13import express from "express" ;
24import dotenv from "dotenv" ;
35// Import cors to allow requests from any origin
@@ -34,12 +36,28 @@ dotenv.config({
3436connectDB ( ) ;
3537//#Initialize middleware
3638const 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
4139app . 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
4563app . use ( "/profile" , userProfileRoutes ) ;
Original file line number Diff line number Diff line change 22import { 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
77if ( ! API_BASE_URL ) {
88 console . error (
Original file line number Diff line number Diff line change 1- import { defineConfig } from "vite" ;
1+ import { defineConfig , loadEnv } from "vite" ;
22import 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 ( / ^ \/ a p i / , "" ) ,
24+ } ,
25+ } ,
26+ } ,
27+ } ;
728} ) ;
You can’t perform that action at this time.
0 commit comments