-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
114 lines (100 loc) · 3.02 KB
/
server.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
const express = require("express");
const fs = require("fs").promises;
const path = require("path");
const cors = require("cors");
const adminData = require("./data/admin.json");
const app = express();
const port = 3000;
app.use(express.json());
app.use(cors());
app.use(express.static("public"));
app.get("/api/items", async (req, res) => {
try {
const data = await fs.readFile(
path.join(__dirname, "data", "items.json"),
"utf8",
);
res.json(JSON.parse(data));
} catch (error) {
res.status(500).json({ error: "Error reading items" });
}
});
app.post("/api/items", async (req, res) => {
try {
const data = await fs.readFile(
path.join(__dirname, "data", "items.json"),
"utf8",
);
const items = JSON.parse(data);
const newItem = req.body;
newItem.id = Math.max(...items.items.map((item) => item.id)) + 1;
items.items.push(newItem);
await fs.writeFile(
path.join(__dirname, "data", "items.json"),
JSON.stringify(items, null, 2),
);
res.json(newItem);
} catch (error) {
res.status(500).json({ error: "Error adding item" });
}
});
app.put("/api/items/:id", async (req, res) => {
try {
const data = await fs.readFile(
path.join(__dirname, "data", "items.json"),
"utf8",
);
const items = JSON.parse(data);
const id = parseInt(req.params.id);
const itemIndex = items.items.findIndex((item) => item.id === id);
if (itemIndex === -1) {
return res.status(404).json({ error: "Item not found" });
}
items.items[itemIndex] = { ...req.body, id };
await fs.writeFile(
path.join(__dirname, "data", "items.json"),
JSON.stringify(items, null, 2),
);
res.json(items.items[itemIndex]);
} catch (error) {
res.status(500).json({ error: "Error updating item" });
}
});
app.delete("/api/items/:id", async (req, res) => {
try {
const data = await fs.readFile(
path.join(__dirname, "data", "items.json"),
"utf8",
);
const items = JSON.parse(data);
const id = parseInt(req.params.id);
items.items = items.items.filter((item) => item.id !== id);
await fs.writeFile(
path.join(__dirname, "data", "items.json"),
JSON.stringify(items, null, 2),
);
res.json({ message: "Item deleted successfully" });
} catch (error) {
res.status(500).json({ error: "Error deleting item" });
}
});
app.post("/api/login", (req, res) => {
const { username, password } = req.body;
if (username === adminData.username && password === adminData.password) {
res.status(200).json({ success: true });
} else {
res.status(401).json({ error: "Invalid credentials" });
}
});
app.get("/admin", (req, res) => {
res.sendFile(path.join(__dirname, "public", "admin.html"));
});
app.get("/customer", (req, res) => {
res.sendFile(path.join(__dirname, "public", "customer.html"));
});
app.use((req, res) => {
res.status(404).sendFile(path.join(__dirname, "public", "404.html"));
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});