-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (46 loc) · 1.46 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
import express from 'express'
import mongoose from 'mongoose'
import dotenv from 'dotenv'
dotenv.config()
import router from './config/router.js'
import path from 'path-posix'
import { fileURLToPath } from 'url'
const app = express()
const port = process.env.PORT
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const startServer = async () => {
try {
// Connect to database
await mongoose.connect(process.env.dbURI)
console.log('🤝 Database successfully connected!')
// MIDDLEWARE
app.use(express.json())
// Router
app.use('/api', router)
// Log every request
app.use((req, _res, next) => {
console.log(`🚨 Request received: ${req.method} - ${req.url}`)
next()
})
// server static assets if in production
if (process.env.NODE_ENV === 'production'){
app.use(express.static('client/build')) // set static folder
//returning frontend for any route other than api
app.get('*',(_req, res)=>{
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
})
}
// Catcher
app.use((_req, res) => {
return res.status(404).json({ message: 'Not found!' })
})
// Start listening for requests
app.listen(port || 4000, '0.0.0.0', () => {
console.log(`🚀 Server up and running on port ${port}`)
})
} catch (err) {
console.log('🆘 Something went wrong - couldnt connect')
console.log(err)
}
}
startServer()