diff options
| author | Yuval Adam <_@yuv.al> | 2025-07-10 13:13:49 +0200 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2025-07-10 13:13:49 +0200 |
| commit | fe46676b321f93c29207bfb19bee6a6aaf4d178a (patch) | |
| tree | 883195dc0fa6eaed35ae999bffc3802cbbf3e66b /src | |
| parent | cb4900df47011a85fd04f9dfc7f90e2f01d88b35 (diff) | |
Add api route
Diffstat (limited to 'src')
| -rw-r--r-- | src/pages/api/hello.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/pages/api/hello.ts b/src/pages/api/hello.ts new file mode 100644 index 0000000..eba198d --- /dev/null +++ b/src/pages/api/hello.ts @@ -0,0 +1,54 @@ +import type { APIRoute } from 'astro'; + +export const GET: APIRoute = async ({ request }) => { + const url = new URL(request.url); + const name = url.searchParams.get('name') || 'World'; + + return new Response( + JSON.stringify({ + message: `Hello, ${name}!`, + timestamp: new Date().toISOString(), + method: 'GET' + }), + { + status: 200, + headers: { + 'Content-Type': 'application/json', + }, + } + ); +}; + +export const POST: APIRoute = async ({ request }) => { + try { + const body = await request.json(); + + return new Response( + JSON.stringify({ + message: 'Data received successfully', + received: body, + timestamp: new Date().toISOString(), + method: 'POST' + }), + { + status: 200, + headers: { + 'Content-Type': 'application/json', + }, + } + ); + } catch (error) { + return new Response( + JSON.stringify({ + error: 'Invalid JSON', + timestamp: new Date().toISOString() + }), + { + status: 400, + headers: { + 'Content-Type': 'application/json', + }, + } + ); + } +};
\ No newline at end of file |
