GET api route

This commit is contained in:
2025-09-09 08:34:19 -04:00
parent 3d2b367bf9
commit 2c02ffcc93
2 changed files with 24 additions and 0 deletions

View File

@@ -1,6 +1,22 @@
<script>
//@ts-nocheck
let { data } = $props();
/** @type {number}*/
let number = $state();
async function roll() {
const res = await fetch('/roll');
number = await res.json();
}
</script>
<h1>Welcome to SvelteKit, {data.visited ? 'friend' : 'stranger'}</h1>
<p>Visit <a href="https://svelte.dev/docs/kit">svelte.dev/docs/kit</a> to read the documentation</p>
<h2 class="text-2xl font-bold">Roll the Dice</h2>
<button class="px-4 py-1 rounded-md bg-blue-500 text-white hover:bg-blue-700 hover:cursor-pointer" onclick={roll}>Roll</button>
{#if number !== undefined}
<p>You rolled a {number}</p>
{/if}

View File

@@ -0,0 +1,8 @@
// @ts-nocheck
import { json } from "@sveltejs/kit";
export function GET() {
const number = Math.floor(Math.random() * 6) + 1;
return json(number)
}