-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (109 loc) · 3.39 KB
/
index.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
const express = require('express');
const uuid = require('uuid');
const app = express();
app.use(express.json());
const port = 3001;
const cors = require('cors');
app.use(cors());
// users registered
const usersList = [];
// middleware's
const showMethodNUrl = (req, res, next) => {
console.log(`${req.method}: ${req.url}`);
next();
};
app.use(showMethodNUrl);
const checkUserId = (req, res, next) => {
// finding user by id
const userId = req.params.id;
const userIndex = usersList.findIndex(user => user.id === userId);
// return error if not found
if (userIndex < 0) {
return res.status(404).json({ error: 'User Not Found' });
}
// incorporating the user index in request
req.userIndex = userIndex;
next();
};
// verify if client is trying to change or add id information
const verifyUserData = (req, res, next) => {
const { id } = req.body;
if (id) {
return res.status(401).json({ error: 'Unable to change id information' });
}
next();
};
// routes
// view all users
app.get('/users', (req, res) => {
try {
return res.json(usersList);
} catch (err) {
console.error(`Something happened: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
// view an specific user
app.get('/users/:id', checkUserId, (req, res) => {
try {
return res.json(usersList[req.userIndex]);
} catch (err) {
console.error(`Something happened: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
// create a user
app.post('/users', verifyUserData, (req, res) => {
/* request(body) pattern:
{
"name": "Leone",
"age": "22"
}
*/
// receiving data
const { name, age } = req.body;
const registeredUser = { name, age, id: uuid.v4() };
// verifying data received
for (userData in registeredUser) {
if (!registeredUser[userData]) return res.status(400).json({ error: 'Missing Data' });
}
// trying to register user
try {
usersList.push(registeredUser);
return res.status(201).json(registeredUser);
} catch (err) {
console.error(`Something happened: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
// updating an user (by id)
app.put('/users/:id', checkUserId, verifyUserData, (req, res) => {
// receiving user data
const { name, age } = req.body;
const newData = { name, age };
try {
// function to update one or more data from an user
for (let userData in usersList[req.userIndex]) {
for (data in newData) {
if (newData[data] && data === userData) usersList[req.userIndex][data] = newData[data];
}
}
return res.json({ updatedUser: usersList[req.userIndex] });
} catch (err) {
console.error(`Something happened: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
// delete an user
app.delete('/users/:id', checkUserId, (req, res) => {
try {
usersList.splice(req.userIndex, 1);
return res.status(204).json();
} catch (err) {
console.error(`Something happened: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
app.listen(process.env.PORT || port, () => {
console.log(`㊗️ - server started in port: ${port}.`);
});