-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
142 lines (128 loc) · 4.15 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
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
136
137
138
139
140
141
142
const express = require('express')
const app = express()
const cors = require('cors') //allows requests from external clients
app.use(cors())
const PORT = 8000
/*
for handling query strings
using insteadhttps://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
*/
// const url = require('url');
// const querystring = require('querystring');
const food = {
"chicken": {
"internalTempF": 140,
"types": {
"full": {
"size": "small",
"count": 1,
"cookTempF": 180,
"cookTimeMinutes": 20,
"notes": ""
},
"breast": {
"size": "medium",
"count": 4,
"cookTempF": 180,
"cookTimeMinutes": 12,
"notes": ""
}
},
},
"pork": {
"internalTempF": 140,
"types": {
"bacon": {
"size": "medium",
"count": 6,
"cookTempF": 180,
"cookTimeMinutes": 12
},
"belly": {
"size": "small",
"count": 1,
"cookTempF": 180,
"cookTimeMinutes": 20
}
},
"beef": {
"internalTempF": 140,
"types": {
"breast": {
"size": "medium",
"count": 4,
"cookTempF": 180,
"cookTimeMinutes": 12
},
"full": {
"size": "small",
"count": 1,
"cookTempF": 180,
"cookTimeMinutes": 20
}
},
},
"unknown": {
"internalTempF": 0,
"types": {
"unknown": {
"size": "unknown",
"count": 0,
"cookTempF": 0,
"cookTimeMinutes": 0
}
},
}
}
}
app.get("/", (request, response) => {
console.log("sending you to index.html")
response.sendFile(`${__dirname}/index.html`)
})
app.get("/api/:category/:type", (request, response) => {
const category = (request.params.category).toLowerCase()
const type = (request.params.type).toLowerCase()
console.log(`searching for Category: ${category}, Type: ${type}`)
//testing
// jsonString = JSON.stringify(food, null, 2)
// console.log(`searching trhough: ${jsonString}`)
if (food[category].types[type]) {
response.json(food[category].types[type])
console.log(food[category].types[type])
} else {
response.json(food['unknown'].types['unknown'])
console.log(food['unknown'].types['unknown'])
}
})
app.get("/lookup", (request, response) => {
// let rawUrl = request.originalUrl;
// const url = request.originalUrl
// const searchParams = new URLSearchParams(url);
// const category = searchParams.get("category")
// const type = searchParams.get("type")
const category = request.query.category
const type = request.query.type
console.log(`Found queries: ${category} and ${type}`)
// console.log(request)
// const category = (request.params.category).toLowerCase()
// const type = (request.params.type).toLowerCase()
// console.log(`searching for Category: ${category}, Type: ${type}`)
//testing
// jsonString = JSON.stringify(food, null, 2)
// console.log(`searching trhough: ${jsonString}`)
if (food[category].types[type]) {
response.json(food[category].types[type])
console.log(food[category].types[type])
} else {
response.json(food['unknown'].types['unknown'])
console.log(food['unknown'].types['unknown'])
}
})
app.get("/api", (request, response) => {
response.json(food)
})
app.listen(process.env.PORT || PORT, () => { //process.env.PORT allows you to use heroku's port number
console.log(`listening on port ${PORT}`)
console.log(`search api using pattern: /api/chicken/breast (replace with category/type, like api/pork/bacon)`)
})
// if getting error that 8000 is used: npx kill-port 8000