Why API routes serve data endpoints
📖 Scenario: You are building a simple Svelte app that needs to get data from the server. Instead of putting data directly in the page, you will create an API route that sends data as JSON. This is how real apps separate data from the page and allow dynamic updates.
🎯 Goal: Create a SvelteKit API route that returns a JSON list of products. Then fetch this data in a Svelte page and display the product names.
📋 What You'll Learn
Create an API route file
src/routes/api/products/+server.js that returns JSON dataDefine a list of products as an array of objects with
id and nameCreate a config variable for the API path in the page component
Fetch the product data from the API route using
fetchDisplay the product names in a list on the page
💡 Why This Matters
🌍 Real World
Web apps often get data from backend APIs. Using API routes to serve JSON lets the frontend fetch data dynamically and update the UI without reloading the whole page.
💼 Career
Understanding API routes and data fetching is essential for frontend and full-stack developers working with modern frameworks like SvelteKit, React, or Next.js.
Progress0 / 4 steps