From 7e9d6bc8aaa41a81b6bcf009eed8946f71702228 Mon Sep 17 00:00:00 2001 From: AlGouvea Date: Mon, 2 Sep 2024 15:02:44 -0300 Subject: [PATCH] =?UTF-8?q?[fix]=20Restaurando=20configura=C3=A7=C3=B5es?= =?UTF-8?q?=20do=20cors=20para=20deploy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/index.js b/src/index.js index 2237e2b..a9c834d 100644 --- a/src/index.js +++ b/src/index.js @@ -10,29 +10,28 @@ const { generateFinancialReportCSV } = require("./Models/csvGenerator"); const app = express(); // Variáveis de ambiente -const { NODE_ENV, MONGO_URI, OFFICIAL_MONGO_URI, PORT } = process.env; +const { NODE_ENV, MONGO_URI, OFFICIAL_MONGO_URI, PORT, FRONT_HOST } = + process.env; console.log("Ambiente:", NODE_ENV); console.log("MONGO_URI:", MONGO_URI); console.log("OFFICIAL_MONGO_URI:", OFFICIAL_MONGO_URI); // Middleware -const allowedOrigins = [ - "http://localhost:5173", - "https://devel--appsentinela.netlify.app", - "https://appsentinela.netlify.app", -]; -app.use( - cors({ - origin: function (origin, callback) { - if (!origin || allowedOrigins.indexOf(origin) !== -1) { - callback(null, true); - } else { - callback(new Error("Not allowed by CORS")); - } - }, - }) -); +const corsOption = { + origin: (origin, callback) => { + const allowedOrigin = FRONT_HOST || "localhost"; + if (origin?.includes(allowedOrigin) || origin === undefined) { + callback(null, true); + } else { + callback(new Error("Not allowed by CORS")); + } + }, + methods: "GET,HEAD,PUT,PATCH,POST,DELETE", +}; + +// Aplicar o middleware CORS antes das rotas +app.use(cors(corsOption)); app.use(bodyParser.json());