-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathserver.js
38 lines (30 loc) · 1.03 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
const path = require('path')
const express = require('express')
require('colors')
require('dotenv').config()
const { errorHandler } = require('./middleware/errorMiddleware')
const connectDB = require('./config/db')
const PORT = process.env.PORT || 5000
// Connect to database
connectDB()
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
// Routes
app.use('/api/users', require('./routes/userRoutes'))
app.use('/api/tickets', require('./routes/ticketRoutes'))
// Serve Frontend
if (process.env.NODE_ENV === 'production') {
// Set build folder as static
app.use(express.static(path.join(__dirname, '../frontend/build')))
// FIX: below code fixes app crashing on refresh in deployment
app.get('*', (_, res) => {
res.sendFile(path.join(__dirname, '../frontend/build/index.html'))
})
} else {
app.get('/', (_, res) => {
res.status(200).json({ message: 'Welcome to the Support Desk API' })
})
}
app.use(errorHandler)
app.listen(PORT, () => console.log(`Server started on port ${PORT}`))