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
// This val will respond with a simple HTML form for users to input their name
export default async function (req: Request): Promise<Response> {
if (req.method === "POST") {
const formData = new URLSearchParams(await req.text());
const name = formData.get("name") ?? "Stranger"; // Default to "Stranger" if no name provided
return new Response(`<h1>Hello, ${name}!</h1>`, { headers: { "Content-Type": "text/html" } });
}
return new Response(`
<html>
<head>
<title>Greetings Form</title>
</head>
<body>
<h1>Welcome!</h1>
<form method="post">
<label for="name">Enter your name:</label><br>
<input type="text" id="name" name="name"><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
`, { headers: { "Content-Type": "text/html" } });
}