From c5ff0923e9ed373f7716420bf52369e33bc2a234 Mon Sep 17 00:00:00 2001 From: "Peter T. Walker" <108812366+petertimwalker@users.noreply.github.com> Date: Tue, 28 May 2024 16:53:38 -0400 Subject: [PATCH] Update server.js --- server.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/server.js b/server.js index c120448..023ff83 100644 --- a/server.js +++ b/server.js @@ -6,29 +6,24 @@ const booksRouter = require('./books'); const cors = require('cors'); const allowedOrigins = ['https://peterwalker.xyz']; -// Middleware to check origin and reject if not allowed -app.use((req, res, next) => { - const origin = req.headers.origin; - console.log(`Request origin: ${origin}`); - if (allowedOrigins.includes(origin)) { - res.setHeader('Access-Control-Allow-Origin', origin); - } - next(); -}); - -// CORS configuration app.use(cors({ origin: (origin, callback) => { if (!origin) { return callback(new Error('Origin not specified'), false); } - if (allowedOrigins.indexOf(origin) === -1) { + if (!allowedOrigins.includes(origin)) { return callback(new Error('Not allowed by CORS'), false); } return callback(null, true); } })); +app.use((req, res, next) => { + const origin = req.headers.origin; + console.log(`Request origin: ${origin}`); + next(); +}); + const isProduction = process.env.NODE_ENV === 'production'; const PORT = isProduction ? 443 : 3001;