-
Notifications
You must be signed in to change notification settings - Fork 0
/
sever.js
54 lines (46 loc) · 1.1 KB
/
sever.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
const express = require("express");
const app = express();
const PORT = 1233;
app.use(express.json());
app.listen(PORT, () => {
console.log(`sever spinning at the door ${PORT}`);
});
let users = [
{
id: 1,
name: "Artur",
email: "[email protected]",
password: "senha123",
role: "admin",
},
{
id: 2,
name: "João",
email: "[email protected]",
password: "123456",
role: "user",
},
];
app.get("/users", (req, res) => {
res.status(200).json(users);
});
app.post("/users", (req, res) => {
const newUser = { id: users.length + 1, ...req.body };
users.push(newUser);
res.status(201).json(newUser);
});
app.put("/users/:id", (req, res) => {
const id = parseInt(req.params.id);
const index = users.findIndex((user) => user.id == id);
if (index !== -1) {
users[index] = { ...users[index], ...req.body };
res.json(users[index]);
} else {
res.status(404).send("User is not found ");
}
});
app.delete("/users/:id", (req, res) => {
const id = parseInt(req.params.id);
users = users.filter((user) => user.id !== id);
res.status(200).send();
});