-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (51 loc) · 1.43 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
const express = require("express");
const app = express();
const port = 8000;
const data = require("./data.json");
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.get("/view", (req, res) => {
res.send(data);
});
app.post("/add", (req, res) => {
var id = data[data.length - 1].id;
id = parseInt(id);
const obj = {
id: `${id + 1}`,
quote: req.body.quote,
author: req.body.author,
};
data.push(obj);
res.send({ msg: `Id = ${id + 1}, Data added successfully` });
});
app.delete("/remove/:id", (req, res) => {
const index = data.findIndex((item) => item.id === req.params.id);
console.log(index);
if (index == -1) {
res.send({ msg: "objectId " + req.params.id + " not found" });
} else {
data.splice(index, 1);
res.send({ msg: "objectId " + req.params.id + " removed successfully" });
}
});
app.put("/update/:id", (req, res) => {
const index = data.findIndex((item) => item.id === req.params.id);
console.log(index);
if (index == -1) {
res.send({ msg: "objectId " + req.params.id + " not found" });
} else {
const obj = {
id: req.params.id,
quote: req.body.quote,
author: req.body.author,
};
data[index] = obj;
res.send({ msg: "objectId " + req.params.id + " updated successfully" });
}
});
app.listen(port, () => {
console.log(`server is running on ${port}`);
});