Skip to content

Commit

Permalink
Update server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
petertimwalker authored May 28, 2024
1 parent 488f5f7 commit c5ff092
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit c5ff092

Please sign in to comment.