-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
55 lines (41 loc) · 1.33 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
import express from 'express'
import mongoose from 'mongoose'
import 'dotenv/config'
import router from './config/router.js'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
// Node setup
const app = express()
// Start server
const startServer = async () => {
try {
await mongoose.connect(process.env.dbURI)
console.log('👋🏽 Database has connected successfully')
// Middleware
app.use(express.json())
// Router middleware
app.use('/api', router)
// ** New lines **
app.use(express.static(path.join(__dirname, 'client', 'build')))
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
})
// Log every request
app.use((req, _res, next) => {
console.log(`🚨 Request received: ${req.method} - ${req.url}`)
next()
})
// Catcher
app.use((_req, res) => {
return res.status(404).json({ message: 'Path not found' })
})
const server = app.listen(process.env.PORT, () => console.log(`🚀 Server up and running on port ${process.env.PORT}`))
server.timeout = 10000 // 10sec server timeout
} catch (err) {
console.log('🚨 Alert - cannot connect')
console.log(err)
}
}
startServer()