-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (40 loc) · 1.29 KB
/
Copy pathindex.js
File metadata and controls
55 lines (40 loc) · 1.29 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
53
54
55
import 'dotenv/config'
import express from 'express';
import db from './db/db.config.js';
import cors from 'cors';
import mainRouter from './src/api/main.routes.js'
import { errorHandler } from './src/middleware/error-handler.js';
const app = express();
const allowedOrigins = [
'http://localhost:5173',
'https://chatgpt-clone-frontend-mu.vercel.app',
'https://vercel.app'
];
app.use(
cors({
origin: allowedOrigins, // Passing the array directly fixes the function edge-case failures
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
})
);
app.use(express.json());
app.use('/api', mainRouter);
// Final middleware for error handling
app.use(errorHandler);
// Define PORT dynamically for production deployment
const PORT = process.env.PORT || 3888;
async function startServer() {
try {
const connection = await db.getConnection();
connection.release();
console.log("Database connected successfully.");
// Listen on the dynamic port instead of a hardcoded one
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
} catch (error) {
console.error('Error starting server:', error);
}
}
startServer();