33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
require('dotenv').config();
|
|
const express = require('express');
|
|
const cors = require('cors');
|
|
const apiRoutes = require('./routes/apiRoutes');
|
|
const userRoutes = require('./routes/userRoutes');
|
|
|
|
const app = express();
|
|
|
|
// Middleware to parse JSON bodies
|
|
app.use(express.json()); // 👈 Add this line
|
|
|
|
// CORS Configuration
|
|
const allowedOrigins = ['http://localhost:4200', 'https://yourfrontend.com','http://localhost:5174']; // Replace as needed
|
|
|
|
app.use(cors({
|
|
origin: function (origin, callback) {
|
|
if (!origin) return callback(null, true); // Allow requests with no origin
|
|
if (allowedOrigins.indexOf(origin) === -1) {
|
|
const msg = 'The CORS policy for this site does not allow access from the specified Origin.';
|
|
return callback(new Error(msg), false);
|
|
}
|
|
return callback(null, true);
|
|
},
|
|
optionsSuccessStatus: 200
|
|
}));
|
|
|
|
// Routes
|
|
app.use('/api/user', userRoutes);
|
|
|
|
// Start server
|
|
const PORT = process.env.PORT || 3000;
|
|
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|