Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
102 lines (88 loc) · 2.2 KB
/
Copy pathindex.js
File metadata and controls
102 lines (88 loc) · 2.2 KB
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
const express = require('express')
const BodyParser = require('body-parser')
const cors = require('cors')
const app = express()
const PORT = process.env.PORT || 3000;
app.use(BodyParser.json())
app.use(BodyParser.urlencoded({ extended: true }))
app.use(cors())
let fruits = ['Banana','Jackfruit', 'Apple', 'Melon', 'Mangosteen', 'Peach', 'Raspberry', 'Blueberry', 'Avocado', 'Grapes','Pomegranite','Tangerine', 'Mango', 'Cherry', 'Tomato','Huckleberry', 'Lychee', 'Durian', 'Blackberry','Raspberry', 'Payapaya','Lemon','Kiwi','Goldenberry','Pineapple']
app.get('/', async (req, res) => {
res.status(200).send({ message: 'hi!' })
});
app.get('/fruits', (req, res) => {
try {
console.log(fruits)
res.status(200).send(fruits)
}
catch (err) {
console.log(err)
}
});
app.post('/fruits', (req, res) => {
try {
const newFruit = req.body.fruit
if (!fruits.includes(newFruit)) {
fruits = [...fruits, newFruit]
res.send(200, 'Created new fruit')
}
else {
res.send(400, "Fruit already exists")
}
}
catch (err) {
console.log(err)
res.sendStatus(500)
}
});
app.delete('/fruits', (req, res) => {
try {
const newFruit = req.body.fruit
const index = fruits.indexOf(newFruit)
if (index != -1) {
fruits.splice(index, 1)
res.sendStatus(200)
}
else {
res.send(400, 'That fruit does not exist')
}
}
catch (err) {
console.log(err)
}
});
app.get('/fruit-check', (req, res) => {
try {
const newFruit = req.query.fruit
if (fruits.includes(newFruit)) {
res.send(200, "Fruit already exists")
}
else {
res.send(200, "Fruit does not exist")
}
}
catch (err) {
console.log(err)
res.sendStatus(500)
}
});
// The product recommendations endpoint
app.get('/recommend', (req, res) => {
const type = req.query.type;
let recommendation;
switch(type) {
case 'fruits':
recommendation = 'Apple';
break;
case 'vegetables':
recommendation = 'Carrot';
break;
default:
recommendation = 'Unknown product type';
}
res.status(200).send({ recommendation });
});
app.listen(PORT, () => {
console.log("Hosted on port " + PORT)
})
module.exports = app;