-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (54 loc) · 1.38 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require('dotenv').config()
const mongoose = require('mongoose')
const express = require('express')
const cors = require('cors')
const morgan = require('morgan')
const path = require('path')
const app = express()
const port = process.env.PORT || 5000
app.disable('x-powered-by')
app.use(express.static(path.join(__dirname, 'client/build')))
app.use(cors())
app.use(express.json())
app.use(morgan('dev'))
app.get('/', async (req, res) => {
try {
const doc = await Candidate.find({}).lean().exec()
return res.status(200).json({ data: doc })
} catch (e) {
console.error(e)
res.status(404)
}
})
app.use('/api/v1', require('./api/v1/routes/assignment.route'))
app.post('/add', async (req, res) => {
try {
const doc = await Candidate.create({ ...req.body })
res.status(201).json({ data: doc })
} catch (e) {
console.error(e)
res.status(400).end()
}
})
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'))
})
const connect = async () => {
return await mongoose
.connect(process.env.MONGOURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log('MongoDB connected')
})
.catch((err) => {
console.log(err)
})
}
connect().then(
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`)
})
)
module.exports = connect