-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
135 lines (106 loc) · 3.1 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict'
const path = require('path')
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const compression = require('compression')
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
const app = express()
const router = express.Router()
// Import sample data
const community = require('./data/community')
const communityInspections = require('./data/community-inspections')
const travelers = require('./data/travelers')
app.set('view engine', 'pug')
if (process.env.NODE_ENV === 'test') {
// NOTE: aws-serverless-express uses this app for its integration tests
// and only applies compression to the /sam endpoint during testing.
router.use('/sam', compression())
} else {
router.use(compression())
}
router.use(cors())
router.use(bodyParser.json())
router.use(bodyParser.urlencoded({ extended: true }))
router.use(awsServerlessExpressMiddleware.eventContext())
// NOTE: tests can't find the views directory without this
app.set('views', path.join(__dirname, 'views'))
router.get('/', (req, res) => {
res.render('index', {
apiUrl: req.apiGateway
? `https://${req.apiGateway.event.headers.Host}/${req.apiGateway.event.requestContext.stage}`
: 'http://localhost:3000'
})
})
router.get('/covid19et', (req, res) => {
res.sendFile(`${__dirname}/covid19et-logo.png`)
})
// API Abstraction
router.get('/v1/:type/:id?', (req, res) => {
const type = req.params.type
if (!type) return res.status(404).json({})
let data = ''
switch (type) {
case 'community':
data = community
break
case 'community-inspections':
data = communityInspections
break
case 'travelers':
data = travelers
break
default:
data = []
break
}
// Get the Id
const id = req.params.id
if (id && data) {
console.log(id)
const getRecord = id => data.find(r => r.id === parseInt(id))
data = getRecord(id)
}
return res.json(data)
})
router.post('/users', (req, res) => {
const user = {
id: ++userIdCounter,
name: req.body.name
}
users.push(user)
res.status(201).json(user)
})
router.put('/users/:userId', (req, res) => {
const user = getUser(req.params.userId)
if (!user) return res.status(404).json({})
user.name = req.body.name
res.json(user)
})
router.delete('/users/:userId', (req, res) => {
const userIndex = getUserIndex(req.params.userId)
if (userIndex === -1) return res.status(404).json({})
users.splice(userIndex, 1)
res.json(users)
})
//
const getRecord = id => users.find(u => u.id === parseInt(userId))
const getUserIndex = userId => users.findIndex(u => u.id === parseInt(userId))
// Ephemeral in-memory data store
const users = [
{
id: 1,
name: 'Joe'
},
{
id: 2,
name: 'Jane'
}
]
let userIdCounter = users.length
// The aws-serverless-express library creates a server and listens on a Unix
// Domain Socket for you, so you can remove the usual call to app.listen.
// app.listen(3000)
app.use('/', router)
// Export your express server so you can import it in the lambda function.
module.exports = app