-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (61 loc) · 1.67 KB
/
index.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
const express = require('express')
const { getProductItems, getProductQuantity } = require('./src/queries')
const compression = require('compression')
const helmet = require('helmet')
const path = require('path')
const HOST = process.env.HOST
const PORT = process.env.PORT
const app = express()
const router = express.Router('/productItems')
// get all product Items.
router.get('/', async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
try {
const productItems = await getProductItems()
const responseData = productItems['records'].map((item) => {
delete item.attributes
return item
})
res.status(200)
res.json(responseData)
} catch (error) {
console.error(error)
res.status(500)
res.json(error.message)
}
})
// get stock for a single productitem
router.get('/:productItemNum', async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
try {
const quantity = await getProductQuantity(req.params.productItemNum)
const responseData = {
quantity: quantity['records'][0].QuantityOnHand,
}
res.status(200)
res.json(responseData)
} catch (error) {
console.error(error)
res.status(500)
res.json(error.message)
}
})
app.use(compression())
app.use(
helmet.contentSecurityPolicy({
useDefaults: true,
directives: {
'script-src': [
"'self'",
'code.jquery.com',
'cdnjs.cloudflare.com',
'stackpath.bootstrapcdn.com',
],
},
}),
)
app.use('/', express.static(path.join(__dirname + '/client/build')))
app.use('/productItems', router)
app.listen(PORT, () => {
console.log('Server Started on ' + HOST + ' on port ' + PORT)
})