-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
34 lines (26 loc) · 1023 Bytes
/
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
const express = require('express')
const mongoose = require('mongoose')
const app = express()
const cors = require('cors')
const thread = require('./routes/threadRoute')
const comment = require('./routes/commentRoute')
const path = require('path')
require('dotenv').config()
const PORT = process.env.PORT || 3001
mongoose.Promise = global.Promise
dbURL = `mongodb+srv://${process.env.USER_NAME}:${process.env.PASSWORD}@cluster0.u079k.mongodb.net/forum-app?retryWrites=true&w=majority`
mongoose.connect(dbURL)
.catch((err) => console.log(err.message))
app.use(cors())
app.use(express.json()) //parse the request coming from the front-end
app.use('/api/thread', thread)
app.use('/api/comment', comment)
if(process.env.NODE_ENV === 'production'){
app.use(express.static('client/build'))
app.get('*', (req,res) =>{
res.sendFile(path.resolve(__dirname, 'client' , 'build', 'index.html'))
})
}
app.listen(PORT, () =>{
console.log(`Server is listening on port ${PORT}`);
})