-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbms.mjs
More file actions
62 lines (56 loc) · 1.99 KB
/
dbms.mjs
File metadata and controls
62 lines (56 loc) · 1.99 KB
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
import express from "express";
import bodyParser from "body-parser";
import DatabaseManager from "./src/models/DatabaseManager.mjs";
const app = express();
const dbManager = new DatabaseManager();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("static"));
app.get("/", async (req, res) => {
await dbManager.initDb();
const users = await dbManager.fetchData("users");
await dbManager.closeDb();
res.send(`
<html>
<head>
<title>DBMS Interface</title>
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<h1>DBMS Interface</h1>
<form action="/add-user" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<button type="submit">Add User</button>
</form>
<h2>All Users</h2>
<ul>
${users
.map(
(user) =>
`<li>${user.username} <a href="/delete-user?id=${user.id}">Delete</a></li>`
)
.join("")}
</ul>
</body>
</html>
`);
});
app.post("/add-user", async (req, res) => {
const { username, password } = req.body;
await dbManager.initDb();
await dbManager.insertData("users", { username, password });
await dbManager.closeDb();
res.redirect("/");
});
app.get("/delete-user", async (req, res) => {
const { id } = req.query;
await dbManager.initDb();
await dbManager.deleteData("users", { id });
await dbManager.closeDb();
res.redirect("/");
});
app.listen(8004, () => {
console.log("Server is running on http://localhost:8004");
});