chris 962201975b fix: Resolve CORS issue for photo uploads
- Forces frontend to use HTTP for backend requests to prevent mixed content errors.
- Tightens backend CORS policy to a whitelist of allowed origins.
2025-11-24 16:39:19 -05:00

45 lines
1.3 KiB
JavaScript

const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const app = express();
const port = process.env.PORT || 5000;
const whitelist = [
'https://preview.beachpartyballoons.com',
'http://localhost:3050',
'http://127.0.0.1:3050',
'http://localhost:8080' // Common local dev port
];
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1 || !origin) { // !origin allows same-origin and server-to-server
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization']
};
app.use(cors(corsOptions));
app.use(express.json());
app.use('/uploads', express.static('uploads'));
// MongoDB Connection
const uri = process.env.MONGO_URI || 'mongodb://localhost:27017/photogallery';
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
const connection = mongoose.connection;
connection.once('open', () => {
console.log("MongoDB database connection established successfully");
})
// API Routes
const photosRouter = require('./routes/photos');
app.use('/photos', photosRouter);
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});