Readme

Guestbook

You can put an interactive guestbook on your website using vals!

This val is the backend of the guestbook that returns existing messages and handles new messages.

To generate a HTML snippet to post on your website, use @vtdocs.generateGuestbookSnippet.

Setup

Fork this val.

Paste the below snippet into your Val Town workspace – replacing alice and guestbook with your Val Town username and the name of @vtdocs.guestbook fork respectively.

@vtdocs.generateGuestbookSnippet('alice', 'guestbook')

Place the HTML block it returns anywhere on your website.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { set } from "https://esm.town/v/std/set?v=11";
let { guestbookMessages } = await import("https://esm.town/v/vtdocs/guestbookMessages");
export const guestbook = async (req: express.Request, res: express.Response) => {
const esc = (await import("npm:escape-html@1.0.3")).default;
if (guestbookMessages === undefined) {
guestbookMessages = [];
}
if (req.method === "GET") {
return res.json(guestbookMessages);
}
if (req.body.name === undefined || req.body.text === undefined) {
return res.status(400);
}
guestbookMessages.push({
name: esc(req.body.name),
text: esc(req.body.text),
});
await set(
"guestbookMessages",
guestbookMessages,
);
return res.status(200);
};
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!
October 23, 2023