janpaul123-valle_tmp_557616596483957206351422557248876.web.val.run
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
// This code will create a simple comment box where users can submit a comment using an input field.
// It will persist the comments using blob storage, and display all previous comments using a GET request to fetch comments.
import { blob } from "https://esm.town/v/std/blob";
import { Hono } from "npm:hono@3";
import { html } from "https://esm.town/v/stevekrouse/html";
const BLOB_KEY = "comments_storage";
const app = new Hono();
app.get("/", async (c) => {
const comments = await blob.getJSON(BLOB_KEY) ?? [];
return c.html(html`
<html>
<body>
<h1>Comments</h1>
<ul>
${comments.map(comment => `<li>${comment}</li>`).join('')}
</ul>
<form action="/" method="POST">
<input name="comment" type="text" placeholder="Enter your comment here" required />
<button type="submit">Submit</button>
</form>
</body>
</html>
`);
});
app.post("/", async (c) => {
const form = await c.req.parseBody();
const newComment = form.comment;
if (newComment) {
const comments = await blob.getJSON(BLOB_KEY) ?? [];
comments.push(newComment);
await blob.setJSON(BLOB_KEY, comments);
}
return c.redirect("/");
});
export default app.fetch;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 17, 2024