-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (40 loc) · 1.37 KB
/
Copy pathserver.js
File metadata and controls
52 lines (40 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const express = require('express');
const connectDB = require('./config/db');
const rateLimit = require('./middleware/rateLimit');
const { swaggerUi, swaggerDocs } = require('./swagger');
const apiRoutes = require('./routes/apiRoutes');
const cors = require('cors');
const path = require('path');
const app = express();
// Connect Database
connectDB();
// Init Middleware
app.use(express.json());
// CORS Middleware
const allowedOrigins = [
'http://localhost:3000',
'http://localhost:5173',
];
app.use(cors({
origin: (origin, callback) => {
if (allowedOrigins.includes(origin) || !origin) {
// Allow requests with no origin (like mobile apps or Postman)
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: ['GET', 'POST', 'PUT', 'DELETE'], // Allow only specific methods
allowedHeaders: ['Content-Type', 'Authorization'], // Allow specific headers
}));
// Enable pre-flight requests for all routes
app.options('*', cors());
// Rate Limiting
app.use(rateLimit);
// Swagger Documentation
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
// Define Routes
app.use('/', apiRoutes);
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));