-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.js
68 lines (59 loc) · 1.32 KB
/
19.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
// CRUD Operation
const express = require("express");
const app = express();
const PORT = 3000;
app.use(express.json());
const books = [
{
id: 1,
title: "Book 1",
author: "Author 1",
},
{
id: 2,
title: "Book 2",
author: "Author 2",
},
{
id: 3,
title: "Book 3",
author: "Author 3",
},
];
//Read Data
app.get("/books", (req, res) => {
res.json(books);
});
//Write Data
app.post("/books", (req, res) => {
const newBook = req.body;
newBook.id = books.length + 1;
books.push(newBook);
res.status(201).json(newBook);
});
//Update Data
app.put("/books/:id", (req, res) => {
const id = parseInt(req.params.id);
const updateData = req.body;
const index = books.findIndex((book) => book.id === id);
if (index !== -1) {
books[index] = { ...books[index], ...updateData };
res.json(books[index]);
} else {
res.status(404).json({ error: "Book not found" });
}
});
app.delete("/books/:id", (req, res) => {
const id = parseInt(req.params.id);
const index = books.findIndex((book) => book.id === id);
if (index !== -1) {
const deletedBook = books[index];
books.splice(index, 1)[0];
res.json(deletedBook);
} else {
res.status(404).json({ error: "Book not found" });
}
});
app.listen(PORT, () => {
console.log(`Server is listing on ${PORT}`);
});