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
// This val responds with an HTML form styled with CSS to input the user's name and greets them upon form submission
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";
const htmlResponse = `
<style>
body { font-family: Arial, sans-serif; text-align: center; }
h1 { color: #333; }
form { margin-top: 20px; }
input[type="text"], input[type="submit"] { padding: 5px; margin: 5px; }
</style>
<h1>Hello, ${name}!</h1>
`;
return new Response(htmlResponse, {
headers: { "Content-Type": "text/html" },
});
} else {
const htmlForm = `
<style>
body { font-family: Arial, sans-serif; text-align: center; }
form { margin-top: 20px; }
input[type="text"], input[type="submit"] { padding: 5px; margin: 5px; }
</style>
<form action="/" method="POST">
<label for="name">Enter your name:</label><br>
<input type="text" id="name" name="name"><br>
<input type="submit" value="Submit">
</form>
`;
return new Response(htmlForm, {
headers: { "Content-Type": "text/html" },
});
}
}