-
Notifications
You must be signed in to change notification settings - Fork 0
/
userRouter.js
64 lines (61 loc) · 1.28 KB
/
userRouter.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
const express = require("express");
const router = express.Router();
// database:
const users = [
{
id: "1",
name: "As'ad",
job: "Programmer",
email: "[email protected]",
password: "test123",
},
{
id: "2",
name: "Laksman",
job: "Programmer",
email: "[email protected]",
password: "test123",
},
{
id: "3",
name: "Adan",
job: "Farmer",
email: "[email protected]",
password: "test123",
},
{
id: "4",
name: "Kurnia",
job: "Miliarder",
email: "[email protected]",
password: "test123",
},
];
// 1
router.get("/user", (req, res) => {
res.json(users);
});
// 2
router.get("/user/search", (req, res) => {
const job = req.query.job;
const filteredUsers = users.filter((user) => user.job === job);
res.json(filteredUsers);
});
// 3
router.get("/user/:id", (req, res) => {
const id = req.params.id;
res.json(users.find((user) => user.id === id));
});
// 4
router.post("/user", (req, res) => {
const newUser = req.body;
users.push(newUser);
res.json({ status: "OK", desc: "user created" });
});
// 5
router.delete("/user/:id", (req, res) => {
const index = users.findIndex((user) => user.id === req.params.id);
delete users[index];
res.json({ status: "OK", desc: "user deleted" });
});
module.exports = router;