-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.mjs
More file actions
46 lines (41 loc) · 1.12 KB
/
main.mjs
File metadata and controls
46 lines (41 loc) · 1.12 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
import express from "express";
import fs from "node:fs";
import { PrismaClient } from "@prisma/client";
const app = express();
app.use(express.urlencoded({ extended: true }));
const prisma = new PrismaClient();
const template = fs.readFileSync("./template.html", "utf-8");
app.get("/", async (request, response) => {
const todos = await prisma.todo.findMany();
response.send(
template.replace(
"<!-- todos -->",
todos
.map(
(todo) => `
<li>
<span>${todo.title}</span>
<form method="post" action="/delete">
<input type="hidden" name="id" value="${todo.id}" />
<button type="submit">削除</button>
</form>
</li>
`
)
.join("")
)
);
});
app.post("/create", async (request, response) => {
await prisma.todo.create({
data: { title: request.body.title },
});
response.redirect("/");
});
app.post("/delete", async (request, response) => {
await prisma.todo.delete({
where: { id: parseInt(request.body.id, 10) },
});
response.redirect("/");
});
app.listen(3000);