-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (36 loc) · 1.06 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
require('dotenv').config()
const express = require('express')
const app = express()
const cors = require('cors')
const mongoose = require('mongoose')
const path = require('path')
const port = process.env.PORT || 3001
const dbUri = process.env.DB_URI
//middlewares
app.use(cors())
app.use(express.json())
//db
mongoose
.connect(dbUri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
})
.then(console.log('DB connected'))
.catch((err) => console.log(err))
mongoose.set('useFindAndModify', false)
app.listen(port, (err) => {
if (err) console.log(err)
console.log(`Server started at ${port}`)
})
//routes
app.use('/api/user', require('./api/routes/user'))
app.use('/api/post', require('./api/routes/post'))
app.use('/api/comment', require('./api/routes/comment'))
// redirect react routes in production build
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'client/build')))
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'))
})
}