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
import { z } from "npm:zod@3.21.4";
import { UserSchema } from "./zod_demo_shared";
async function server(request: Request): Promise<Response> {
if (request.method === "POST" && new URL(request.url).pathname === "/register") {
try {
const body = await request.json();
const validatedUser = UserSchema.parse(body);
// Here you would typically save the user to a database
// For this demo, we'll just return a success message
return new Response(JSON.stringify({ message: "User registered successfully!" }), {
headers: { "Content-Type": "application/json" },
});
} catch (error) {
if (error instanceof z.ZodError) {
return new Response(JSON.stringify({ errors: error.errors }), {
status: 400,
headers: { "Content-Type": "application/json" },
});
}
return new Response("Internal Server Error", { status: 500 });
}
}
return new Response(
`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zod Demo</title>
<style>${css}</style>
</head>
<body>
<div id="root"></div>
<script type="module" src="https://esm.town/v/stevekrouse/zod_demo_frontend"></script>
</body>
</html>
`,
{
headers: { "Content-Type": "text/html" },
},
);
}
const css = `
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
form div {
margin-bottom: 10px;
}
label {
display: block;
margin-bottom: 5px;
}
input {
width: 100%;
padding: 5px;
}
.error {
color: red;
font-size: 0.8em;
}
.server-response {
margin-top: 20px;
padding: 10px;
background-color: #f0f0f0;
border: 1px solid #ddd;
}
`;
export default server;