-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
66 lines (52 loc) · 1.46 KB
/
app.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
66
'use strict'
require('dotenv').config()
const bodyParser = require('body-parser')
const express = require('express')
const morgan = require('morgan')
const config = require('./config')
const dataService = require('./services/dataService')
const LunchCrew = require('./models/LunchCrew')
const app = express()
app.use((request, response, next) => {
response.header('Access-Control-Allow-Origin', '*')
response.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
app.use(bodyParser.json())
app.use(morgan('dev'))
app.use(express.static('public'))
app.get('/', (request, response) => {
response.send('lunchlotto')
})
app.get('/lunchCrew', (request, response) => {
dataService.getLunchCrews()
.then(lunchCrews => {
response.send(lunchCrews)
})
.catch(error => {
response.status(500).send(error)
})
})
app.post('/lunchCrew', (request, response) => {
var lunchCrew = new LunchCrew(request.body)
if (!lunchCrew.validate()) {
response.status(400).send(`Failed to validate ${JSON.stringify(request.body)}.`)
return
}
dataService.insertLunchCrew(lunchCrew)
.then(() => {
response.sendStatus(200)
})
.catch(error => {
response.status(500).send(error)
})
})
dataService.connect(config.dbUrl)
.then(() => {
app.listen(config.appPort, () => {
console.log(`App running on port ${config.appPort}`)
})
})
.catch(error => {
console.error(error)
})