-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (86 loc) · 3 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const corsOptions = require('./src/config/corsOptions')
// const https = require('https')
// const httpsOptions = require('./src/config/httpsOptions')
const session = require('express-session')
const connectSequelizeSessionConfig = require('./src/config/connectSequelizeSession')
const { updateLoggerOptions } = require('./src/utils/updateLogger')
const expressWinston = require('express-winston')
const { customLogger } = require('./src/utils/loggerTransport')
const { sequelize } = require('./src/models')
const { publicRouter, apiRouter, userAuthRouter, dashboardRouter } = require('./src/routes')
require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` })
const { hostName, portNumber, baseURLServer } = require('./src/config/serverConfig')
require('colors')
const app = express()
const HOST = process.env.HOST || hostName
const PORT = process.env.PORT || portNumber
const BASE_URL = process.env.BASE_URL_SERVER || baseURLServer
app.set('trust proxy', 1)
// Enable CORS for all origins
app.use(cors(corsOptions))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
// Use pg as the session store
app.use(session(connectSequelizeSessionConfig))
// Custom middleware to store log activity
app.use((req, res, next) => {
try {
updateLoggerOptions(req)
next()
} catch (error) {
// console.error('Error logging request:', error)
next(error)
}
})
// Express middleware for logging with Winston
app.use(
expressWinston.logger({
winstonInstance: customLogger,
statusLevels: true,
meta: true,
expressFormat: true,
colorize: false
})
)
// Define routes
app.use('/', publicRouter)
app.use('/', apiRouter)
app.use('/', userAuthRouter)
app.use('/', dashboardRouter)
// Error handling middleware
app.use((error, req, res, next) => {
// Send a more detailed error response in development
if (process.env.NODE_ENV === 'development') {
console.error(error.stack)
res.status(500).json({ error: error.message, stack: error.stack })
} else {
res.status(500).send('Something went wrong!')
}
})
// Sync database and start server
sequelize.sync().then(() => {
console.log('Database synchronized successfully'.green)
if (process.env.NODE_ENV === 'production') {
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`.blue)
})
// https test on local for production environment
// https.createServer(httpsOptions, app).listen(PORT, HOST, () => {
// console.log(`Server is running on ${BASE_URL}`.blue)
// })
} else {
app.listen(PORT, HOST, () => {
console.log(`Server is running on ${BASE_URL}`.blue)
})
}
})
// Handle Promise Rejections
// if (process.env.NODE_ENV !== 'production') {
// process.on('unhandledRejection', (reason, promise) => {
// console.error('Unhandled Rejection at:', promise, 'reason:', reason)
// // Handle the error, close database connections, etc.
// })
// }