-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (65 loc) · 1.96 KB
/
server.js
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
73
74
75
76
77
78
79
80
81
82
import path from 'path'
import express from 'express'
import dotenv from 'dotenv'
import morgan from 'morgan'
import cors from 'cors'
// swagger docs
import swaggerUi from 'swagger-ui-express'
import swaggerJSDoc from 'swagger-jsdoc'
import { notFound, errorHandler } from './middleware/Error.js'
import connectDB from './config/db.js'
import userRoutes from './routes/UserRoutes.js'
import bankRoutes from './routes/BankRoutes.js'
dotenv.config()
connectDB()
const app = express()
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}
const corsOptions = {
origin: 'http://localhost:4000', // Cors for svelte app running on port 4000
credentials: true, // Allow cookies for cross-origin requests (if needed)
optionSuccessStatus: 200, // Optional: Set the HTTP status code for preflight requests
};
app.use(express.json())
app.use(cors(corsOptions));
// Swagger docs
const swaggerOptions = {
swaggerDefinition: {
info: {
title: 'Bank API',
description: 'Bank API Information',
contact: {
name: 'Amazing Developer',
},
servers: ['http://localhost:5000'],
},
},
apis: ['./routes/*.js'],
}
const swaggerDocs = swaggerJSDoc(swaggerOptions)
app.use('/api/users', userRoutes)
app.use('/api/banks', bankRoutes)
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs))
// Apply CORS middleware to all routes
const __dirname = path.resolve()
app.use('/uploads', express.static(path.join(__dirname, '/uploads')))
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'frontend/build')))
app.get('/', (req, res) =>
res.sendFile(path.resolve(__dirname, 'frontend/build', 'index.html'))
)
} else {
app.get('/', (req, res) => {
res.sendFile('API is running....')
})
}
app.use(notFound)
app.use(errorHandler)
const PORT = process.env.PORT || 5000
app.listen(
PORT,
console.log(
`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`
)
)