What is the main purpose of API routes in a Svelte application?
API routes serve as special endpoints that provide data to the frontend. They let your app fetch or send data without loading a new page, like ordering food from a menu without leaving your table.
Click to reveal answer
beginner
How do API routes differ from regular page routes in Svelte?
Regular page routes show user interfaces (pages), while API routes only send or receive data. Think of page routes as the restaurant dining area and API routes as the kitchen sending food orders.
Click to reveal answer
intermediate
Why is it useful to separate data endpoints from UI in Svelte apps?
Separating data endpoints keeps your app organized and faster. The UI focuses on showing info, and API routes handle data behind the scenes, like a waiter bringing food while you enjoy your meal.
Click to reveal answer
beginner
What kind of data can API routes serve in Svelte?
API routes can serve JSON, text, or any data your app needs. For example, user info, product lists, or messages. They act like a data vending machine you can ask anytime.
Click to reveal answer
intermediate
How do API routes improve user experience in Svelte apps?
By serving data quickly without full page reloads, API routes make apps feel smooth and fast. It’s like getting your coffee refill without leaving your seat.
Click to reveal answer
What do API routes in Svelte primarily serve?
AUser interface pages
BStatic images
CData endpoints
DCSS styles
✗ Incorrect
API routes are designed to serve data, not UI or static files.
Which of these best describes the role of API routes?
AThey handle data requests and responses
BThey render HTML pages
CThey manage CSS styling
DThey store images
✗ Incorrect
API routes handle data communication between frontend and backend.
Why separate API routes from page routes in Svelte?
ATo load images faster
BTo combine CSS and JavaScript
CTo avoid using JavaScript
DTo keep data handling and UI separate
✗ Incorrect
Separating concerns helps keep code organized and efficient.
What format do API routes commonly use to send data?
AJSON
BHTML
CCSS
DSVG
✗ Incorrect
JSON is the most common format for data exchange in APIs.
How do API routes improve app speed?
ABy loading all images at once
BBy sending data without reloading pages
CBy reducing font sizes
DBy disabling JavaScript
✗ Incorrect
API routes allow data updates without full page reloads, making apps faster.
Explain why API routes serve data endpoints in Svelte and how this benefits the app.
Think about how a waiter brings food without you leaving your seat.
You got /4 concepts.
Describe the difference between API routes and page routes in Svelte.
Compare the kitchen and dining area in a restaurant.
You got /4 concepts.
Practice
(1/5)
1. Why do API routes in SvelteKit serve data endpoints instead of full HTML pages?
easy
A. To replace the need for any frontend code
B. To handle user authentication only
C. To serve static files like images and stylesheets
D. To provide data that frontend components can use without reloading the whole page
Solution
Step 1: Understand the purpose of API routes
API routes are designed to send data, not full pages, so frontend can update parts dynamically.
Step 2: Compare with full page reloads
Serving data endpoints avoids reloading the entire page, improving user experience and speed.
Final Answer:
To provide data that frontend components can use without reloading the whole page -> Option D
Quick Check:
API routes serve data, not full pages [OK]
Hint: API routes send data, not full pages, for smooth updates [OK]
Common Mistakes:
Thinking API routes serve full HTML pages
Confusing API routes with static file servers
Assuming API routes only handle authentication
2. Which of the following is the correct way to define a GET handler in a SvelteKit API route?
easy
A. export function get() { return { body: { message: 'Hello' } }; }
B. export async function GET() { return new Response('Hello'); }
C. function GET() { return 'Hello'; }
D. export const get = () => 'Hello';
Solution
Step 1: Recall SvelteKit API route syntax
Handlers must be exported functions named after HTTP methods in uppercase, e.g., GET.
Step 2: Check return type
Handlers return a Response object or an object with status and body; returning a string alone is invalid.
Final Answer:
export async function GET() { return new Response('Hello'); } -> Option B
Quick Check:
Use uppercase method names and return Response [OK]
Hint: Use uppercase method names and return Response objects [OK]
Common Mistakes:
Using lowercase method names like get instead of GET
Returning plain strings instead of Response or object
Not exporting the handler function
3. Given this SvelteKit API route code, what will be the JSON response body?
A. The function should return a Response object or use 'return json()' helper
B. The return object should use 'json' instead of 'body'
C. The function must be async
D. The function name should be lowercase 'get'
Solution
Step 1: Check return type requirements
SvelteKit expects API handlers to return a Response object or use the 'json()' helper for JSON responses.
Step 2: Identify incorrect return format
Returning { body: { message: 'Hi' } } alone is not valid; it must be wrapped properly.
Final Answer:
The function should return a Response object or use 'return json()' helper -> Option A
Quick Check:
Return Response or use json() helper [OK]
Hint: Return Response or use json() helper for JSON data [OK]
Common Mistakes:
Returning plain objects without wrapping
Using lowercase function names
Assuming async is always required
5. You want to create an API route in SvelteKit that returns a list of users as JSON. Which approach correctly serves this data endpoint with proper headers and JSON format?