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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// 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;
background-color: #f0f0f0;
text-align: center;
}
h1 {
color: #333;
}
form {
margin: 50px auto;
padding: 20px;
width: 300px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
input[type="text"] {
width: 100%;
padding: 8px;
margin: 5px 0;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 3px;
}
input[type="submit"] {
width: 100%;
padding: 8px;
margin: 5px 0;
box-sizing: border-box;
border: none;
border-radius: 3px;
background-color: #4CAF50;
color: white;
cursor: pointer;
}
</style>
<h1>Hello, ${name}!</h1>
`;
return new Response(htmlResponse, {
headers: { "Content-Type": "text/html" },
});
} else {
const htmlForm = `
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
text-align: center;
}
form {
margin: 50px auto;
padding: 20px;
width: 300px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
input[type="text"] {
width: 100%;
padding: 8px;
margin: 5px 0;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 3px;
}
input[type="submit"] {
width: 100%;
padding: 8px;
margin: 5px 0;
box-sizing: border-box;
border: none;
border-radius: 3px;
background-color: #4CAF50;
color: white;
cursor: pointer;
}
</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" },
});
}
}