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" },
});
}
}