mirror of
https://github.com/donavon04/DocuCenter.git
synced 2025-01-18 09:40:56 -07:00
51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
|
require('dotenv').config();
|
||
|
const express = require('express');
|
||
|
const cors = require('cors');
|
||
|
const connectDB = require('./src/utils/database');
|
||
|
//Import routes
|
||
|
const userRoutes = require('./src/routes/userRoutes');
|
||
|
const authRoutes = require('./src/routes/authRoutes');
|
||
|
const documentRoutes = require('./src/routes/documentRoutes');
|
||
|
|
||
|
const app = express();
|
||
|
|
||
|
app.use(express.urlencoded({ extended: true })); // Parses form data
|
||
|
app.use(express.json()); // Parses JSON data
|
||
|
|
||
|
// Configure CORS to allow specific origins
|
||
|
const allowedOrigins = [
|
||
|
'https://docucenter.mpe.ca', // Production frontend
|
||
|
'http://localhost:5173', // Development frontend
|
||
|
];
|
||
|
|
||
|
const corsOptions = {
|
||
|
origin: (origin, callback) => {
|
||
|
// Allow requests with no origin (e.g., mobile apps or curl)
|
||
|
if (!origin || allowedOrigins.includes(origin)) {
|
||
|
callback(null, true);
|
||
|
} else {
|
||
|
callback(new Error('Not allowed by CORS'));
|
||
|
}
|
||
|
},
|
||
|
methods: ['GET', 'POST', 'PUT', 'DELETE'], // Specify allowed HTTP methods
|
||
|
allowedHeaders: ['Content-Type', 'Authorization'], // Specify allowed headers
|
||
|
credentials: true, // Enable cookies and credentials sharing
|
||
|
};
|
||
|
|
||
|
app.use(cors(corsOptions));
|
||
|
|
||
|
// Connect to database
|
||
|
connectDB();
|
||
|
|
||
|
// Middleware
|
||
|
app.use(express.json());
|
||
|
|
||
|
// Routes
|
||
|
app.use('/api/user', userRoutes);
|
||
|
app.use('/api/auth', authRoutes);
|
||
|
app.use('/api/document', documentRoutes);
|
||
|
|
||
|
const PORT = process.env.PORT || 3000;
|
||
|
app.listen(PORT, () => {
|
||
|
console.log(`Server running on port ${PORT}`);
|
||
|
});
|