SvelteKit's unified routing means you can create pages and API endpoints using the same folder structure. This reduces complexity and keeps related code together.
The +page.svelte file defines the visible page, while +server.js handles backend logic like API calls or form actions. Both work together to provide a full experience.
The +page.server.js file's load function runs on the server before the page renders, allowing secure and fast data fetching. This integrates backend logic with frontend rendering.
+server.js files export functions named after HTTP methods (GET, POST, etc.) to handle backend requests. The POST function processes form data or API calls on the server.
export async function POST({ request }) { const formData = await request.formData(); // process data return new Response('Success'); }
For SvelteKit to call the POST function in +server.js, the form must specify method="POST". Without it, the browser sends a GET request, so the POST function is never called.