-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (54 loc) · 1.52 KB
/
index.js
File metadata and controls
72 lines (54 loc) · 1.52 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Import Express
import express from 'express'
// Import dotenv
import dotenv from 'dotenv'
// Import testRoutes
import testRoutes from './routes/testRoutes.js'
// Import productRoutes
import productRoutes from './routes/productRoutes.js'
// Import userRoutes
import userRoutes from './routes/userRoutes.js'
// Import connectDB
import connectDB from './utils/db.js'
// Import swagger
import setupSwagger from './utils/swagger.js'
// Import CORS
import cors from 'cors'
// Load environment variables
dotenv.config()
// Create a new Express application
const app = express()
// Use JSON middleware
app.use(express.json())
// Middleware for CORS
app.use(cors(
{
// origin: 'http://localhost:4200, http://www.example.com, http://127.0.0.1:5500',
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
allowedHeaders: ['Content-Type', 'Authorization'],
}
))
// Setup Swagger
setupSwagger(app)
// Use testRoutes
app.use('/api', testRoutes)
// Use productRoutes
app.use('/api/products', productRoutes)
// Use userRoutes
app.use('/api/users', userRoutes)
// เริ่มต้น server
const startServer = async () => {
try {
// เชื่อมต่อ MongoDB ก่อน
await connectDB()
// จากนั้นค่อยเริ่ม server
app.listen(process.env.PORT, () => {
console.log(`Server started on http://${process.env.HOST}:${process.env.PORT}`)
})
} catch (error) {
console.error('Failed to start server:', error)
process.exit(1)
}
}
startServer()