-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
163 lines (151 loc) · 2.8 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const express = require('express');
const bodyParser = require('body-parser');
const CORS = require('cors');
const app = express();
const token =
'ahuBHejkJJiMDhmODZhZi0zaeLTQ4ZfeaseOGZgesai1jZWYgrTA07i73Gebhu98';
app.use(bodyParser.json());
app.use(CORS());
let colors = [
{
color: 'aliceblue',
code: {
hex: '#f0f8ff',
},
id: 1,
},
{
color: 'limegreen',
code: {
hex: '#99ddbc',
},
id: 2,
},
{
color: 'aqua',
code: {
hex: '#00ffff',
},
id: 3,
},
{
color: 'aquamarine',
code: {
hex: '#7fffd4',
},
id: 4,
},
{
color: 'lilac',
code: {
hex: '#9a99dd',
},
id: 5,
},
{
color: 'softpink',
code: {
hex: '#dd99ba',
},
id: 6,
},
{
color: 'bisque',
code: {
hex: '#dd9a99',
},
id: 7,
},
{
color: 'softyellow',
code: {
hex: '#dcdd99',
},
id: 8,
},
{
color: 'blanchedalmond',
code: {
hex: '#ffebcd',
},
id: 9,
},
{
color: 'blue',
code: {
hex: '#6093ca',
},
id: 10,
},
{
color: 'blueviolet',
code: {
hex: '#8a2be2',
},
id: 11,
},
];
let nextId = 12;
function authenticator(req, res, next) {
const { authorization } = req.headers;
if (authorization === token) {
next();
} else {
res.status(403).json({ error: 'User must be logged in to do that.' });
}
}
app.post('/api/login', (req, res) => {
const { username, password } = req.body;
if (username === 'Bubbles' && password === 'Bubbles') {
req.loggedIn = true;
setTimeout(() => {
res.status(200).json({
payload: token,
});
}, 1000);
} else {
res
.status(403)
.json({ error: 'Username or Password incorrect. Please see Readme' });
}
});
app.get('/api/colors', authenticator, (req, res) => {
res.send(colors);
});
app.post('/api/colors', authenticator, (req, res) => {
if (req.body.color !== undefined && req.body.code !== undefined) {
const newcolor = req.body;
newcolor.id = nextId;
colors.push(newcolor);
}
nextId = nextId + 1;
res.status(201).json(colors);
});
app.put('/api/colors/:id', authenticator, (req, res) => {
if (!req.params.id)
res.status(400).send('Your request is missing the color id');
if (req.body.id === undefined || !req.body.color || !req.body.code) {
res
.status(422)
.send('Make sure your request body has all the fields it needs');
}
colors = colors.map((color) => {
if (`${color.id}` === req.params.id) {
return req.body;
}
return color;
});
res.status(200).send(req.body);
});
app.delete('/api/colors/:id', authenticator, (req, res) => {
if (!req.params.id)
res.status(400).send('Your request is missing the color id');
colors = colors.filter((color) => `${color.id}` !== req.params.id);
res.status(202).send(req.params.id);
});
app.get('/', function (req, res) {
res.send('App is working 👍');
});
app.listen(5000, () => {
console.log('Server listening on port 5000');
});