-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (37 loc) · 1.06 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
const express = require('express')
const app = express()
const cors = require('cors')
const PORT = 8000
let thaiGroceries = require('./api')
const kebabCase = require('lodash.kebabcase')
app.use(cors())
const groceryStore = {}
// Using the kebabCase
Object.keys(thaiGroceries).forEach((product) => {
groceryStore[kebabCase(product)] = {
...thaiGroceries[product],
}
})
// Response to locallhost 8000
app.get('/', (request, response) => {
response.sendFile(__dirname + '/index.html')
})
// Response to the other route
app.get(
'/api/thai-grocery-product/:thaiGroceriesItemName',
(request, response) => {
const thaiGroceriesItem = request.params.thaiGroceriesItemName
if (!groceryStore[thaiGroceriesItem]) {
return response.json(groceryStore['unknown'])
}
return response.json(groceryStore[thaiGroceriesItem])
}
)
// All Thai Groceries
app.get('/api/thai-grocery-product', (request, response) => {
return response.json(groceryStore)
})
// process.env.PORT for Heroku
app.listen(process.env.PORT || PORT, () => {
console.log(`Server is running ${PORT}`)
})