-
Notifications
You must be signed in to change notification settings - Fork 0
/
jellyBeanAPI.js
128 lines (111 loc) · 2.88 KB
/
jellyBeanAPI.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
const express = require('express');
const app = express();
app.use(express.static('public'));
const PORT = process.env.PORT || 4001;
const jellybeanBag = {
mystery: {
number: 4
},
lemon: {
number: 5
},
rootBeer: {
number: 25
},
cherry: {
number: 3
},
licorice: {
number: 1
}
};
// Logging Middleware
app.use((req, res, next) => {
console.log(`${req.method} Request Received`);
next('Bag with that name does not exist');
});
app.use('/beans/:beanName', (req, res, next) => {
const beanName = req.params.beanName;
if (!jellybeanBag[beanName]) {
res.status(404).send('Bag with that name does not exist');
return console.log('Response Sent');
}
req.bean = jellybeanBag[beanName];
req.beanName = beanName;
next();
});
app.get('/beans/', (req, res, next) => {
res.send(jellybeanBag);
console.log('Response Sent');
});
app.post('/beans/', (req, res, next) => {
let bodyData = '';
req.on('data', (data) => {
bodyData += data;
});
req.on('end', () => {
const body = JSON.parse(bodyData);
const beanName = body.name;
if (jellybeanBag[beanName] || jellybeanBag[beanName] === 0) {
return res.status(404).send('Bag with that name already exists!');
}
const numberOfBeans = Number(body.number) || 0;
jellybeanBag[beanName] = {
number: numberOfBeans
};
res.send(jellybeanBag[beanName]);
console.log('Response Sent');
});
});
app.get('/beans/:beanName', (req, res, next) => {
res.send(req.bean);
console.log('Response Sent');
});
app.post('/beans/:beanName/add', (req, res, next) => {
let bodyData = '';
req.on('data', (data) => {
bodyData += data;
});
req.on('end', () => {
const numberOfBeans = Number(JSON.parse(bodyData).number) || 0;
req.bean.number += numberOfBeans;
res.send(req.bean);
console.log('Response Sent');
});
});
app.post('/beans/:beanName/remove', (req, res, next) => {
let bodyData = '';
req.on('data', (data) => {
bodyData += data;
});
req.on('end', () => {
const numberOfBeans = Number(JSON.parse(bodyData).number) || 0;
if (req.bean.number < numberOfBeans) {
return res.status(400).send('Not enough beans in the jar to remove!');
}
req.bean.number -= numberOfBeans;
res.send(req.bean);
console.log('Response Sent');
});
});
app.delete('/beans/:beanName', (req, res, next) => {
jellybeanBag[req.beanName] = null;
res.status(204).send();
console.log('Response Sent');
});
app.put('/beans/:beanName/name', (req, res, next) => {
let bodyData = '';
req.on('data', (data) => {
bodyData += data;
});
req.on('end', () => {
const newName = JSON.parse(bodyData).name;
jellybeanBag[newName] = req.bean;
jellybeanBag[req.beanName] = null;
res.send(jellybeanBag[newName]);
console.log('Response Sent');
});
});
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});