From 01a724caf1c76cb7ee3769344ffbd43be871085b Mon Sep 17 00:00:00 2001 From: Mohammad Shahid Beigh <85876937+MohammadShahidBeigh@users.noreply.github.com> Date: Sun, 20 Oct 2024 14:54:43 +0530 Subject: [PATCH] cors update --- mindvault-backend/src/server.ts | 28 ++++++++++++++++++++-------- src/views/pages/Home.tsx | 2 +- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/mindvault-backend/src/server.ts b/mindvault-backend/src/server.ts index 3122e7c..c25d66b 100644 --- a/mindvault-backend/src/server.ts +++ b/mindvault-backend/src/server.ts @@ -17,8 +17,14 @@ dotenv.config(); const app: Application = express(); -// Enable CORS -app.use(cors()); +// Allow specific origins (like your frontend) +app.use( + cors({ + origin: ["http://43.205.10.7:3000"], // Add your frontend's public IP address here + methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], // Methods you want to allow + credentials: true, // Allow cookies and other credentials + }) +); // Use morgan for logging requests app.use(morgan("combined")); // You can change the format as needed @@ -113,12 +119,18 @@ async function startApolloServer() { server.applyMiddleware({app}); // Start Express server - const port = process.env.PORT || 4000; - app.listen({port}, () => { - console.log( - `🚀 Server ready at http://localhost:${port}${server.graphqlPath}` - ); - }); + if (process.env.NODE_ENV === "production") { + app.listen(4000, "0.0.0.0", () => { + console.log("Server running on http://0.0.0.0:4000"); + }); + } else { + const port = process.env.PORT || 4000; + app.listen({port}, () => { + console.log( + `🚀 Server ready at http://localhost:${port}${server.graphqlPath}` + ); + }); + } } startApolloServer(); diff --git a/src/views/pages/Home.tsx b/src/views/pages/Home.tsx index 44f1f1b..28f28e3 100644 --- a/src/views/pages/Home.tsx +++ b/src/views/pages/Home.tsx @@ -68,7 +68,7 @@ const Home: React.FC = () => { fontWeight="bold" color="primary" > - Welcome to MindVaultt + Welcome to MindVault