-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.mjs
More file actions
28 lines (24 loc) · 778 Bytes
/
main.mjs
File metadata and controls
28 lines (24 loc) · 778 Bytes
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
import fs from "node:fs";
import express from "express";
import { PrismaClient } from "@prisma/client";
import escapeHTML from "escape-html";
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.static("static"));
const prisma = new PrismaClient();
const template = fs.readFileSync("./template.html", "utf-8");
app.get("/", async (request, response) => {
const posts = await prisma.post.findMany();
const html = template.replace(
"<!-- posts -->",
posts.map((post) => `<li>${escapeHTML(post.message)}</li>`).join(""),
);
response.send(html);
});
app.post("/send", async (request, response) => {
await prisma.post.create({
data: { message: request.body.message },
});
response.redirect("/");
});
app.listen(3000);