Handle contact form POST with onRequest
This commit is contained in:
parent
0f4035b661
commit
92848ce20d
1 changed files with 11 additions and 1 deletions
|
|
@ -31,7 +31,7 @@ const parseConnectionString = (connectionString: string) => {
|
|||
};
|
||||
};
|
||||
|
||||
export const onRequestPost: PagesFunction<Env> = async ({ request, env }) => {
|
||||
const handlePost: PagesFunction<Env> = async ({ request, env }) => {
|
||||
const formData = await request.formData();
|
||||
const name = readFormField(formData.get("name"));
|
||||
const email = readFormField(formData.get("email"));
|
||||
|
|
@ -147,3 +147,13 @@ export const onRequestPost: PagesFunction<Env> = async ({ request, env }) => {
|
|||
|
||||
return jsonResponse({ ok: true });
|
||||
};
|
||||
|
||||
export const onRequest: PagesFunction<Env> = async (context) => {
|
||||
if (context.request.method !== "POST") {
|
||||
return jsonResponse(
|
||||
{ ok: false, error: "Method not allowed." },
|
||||
405,
|
||||
);
|
||||
}
|
||||
return handlePost(context);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue