Skip to content

Commit

Permalink
adjustment to CORS policy
Browse files Browse the repository at this point in the history
  • Loading branch information
Amarjha01 committed Sep 1, 2024
1 parent 49d94d7 commit 30f7978
Showing 1 changed file with 57 additions and 23 deletions.
80 changes: 57 additions & 23 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,57 @@
// import express from "express";
// import cors from "cors";
// import cookieParser from 'cookie-parser'
// import "dotenv/config";
// import dbconnect from "./config/database/dbconnect.js";
// import router from "./routes/routes.js";

// const app = express();
// const PORT = process.env.PORT || 5000;

// // Middleware
// const allowedOrigins = [
// // process.env.FRONTEND_URL,
// // process.env.FRONTEND_URL2,
// // 'http://localhost:5173',
// 'https://www.electramart.ninja',
// 'https://electramart.ninja'
// ];

// app.use(cors({
// origin: function (origin, callback) {
// if (!origin || allowedOrigins.indexOf(origin) !== -1) {
// callback(null, true);
// } else {
// callback(new Error('Not allowed by CORS'));
// }
// },
// credentials: true,
// methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
// allowedHeaders: ['Content-Type', 'Authorization'],
// preflightContinue: false,
// optionsSuccessStatus: 204
// }));
// app.options('*', cors({
// origin: allowedOrigins,
// credentials: true,
// methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
// allowedHeaders: ['Content-Type', 'Authorization']
// }));

// app.use(express.json());
// app.use(cookieParser())

// app.use("/api", router);

// dbconnect().then(() => {
// app.listen(PORT, '0.0.0.0',() => {
// console.log(`Server is running on port: ${PORT}`);
// });
// });

import express from "express";
import cors from "cors";
import cookieParser from 'cookie-parser'
import cookieParser from 'cookie-parser';
import "dotenv/config";
import dbconnect from "./config/database/dbconnect.js";
import router from "./routes/routes.js";
Expand All @@ -10,41 +61,24 @@ const PORT = process.env.PORT || 5000;

// Middleware
const allowedOrigins = [
// process.env.FRONTEND_URL,
// process.env.FRONTEND_URL2,
// 'http://localhost:5173',
'https://www.electramart.ninja/',
'https://electramart.ninja/'
'https://www.electramart.ninja',
'https://electramart.ninja'
];

app.use(cors({
origin: function (origin, callback) {
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
preflightContinue: false,
optionsSuccessStatus: 204
}));
app.options('*', cors({
origin: allowedOrigins,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
allowedHeaders: ['Content-Type', 'Authorization'],
}));

app.use(express.json());
app.use(cookieParser())
app.use(cookieParser());

app.use("/api", router);

dbconnect().then(() => {
app.listen(PORT, '0.0.0.0',() => {
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on port: ${PORT}`);
});
});

0 comments on commit 30f7978

Please sign in to comment.