Challenge - 5 Problems
Server Routes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What does this +server.js GET route return?
Consider this SvelteKit +server.js file with a GET handler. What is the JSON response when you fetch this route?
Svelte
export async function GET() { return new Response(JSON.stringify({ message: 'Hello from server' }), { headers: { 'Content-Type': 'application/json' } }); }
Attempts:
2 left
💡 Hint
The GET function returns a Response with JSON string and content-type header.
✗ Incorrect
The GET handler returns a Response object with a JSON string containing the message key. The client receives the JSON object {"message":"Hello from server"}.
📝 Syntax
intermediate1:30remaining
Which +server.js POST handler syntax is correct?
You want to create a POST handler in +server.js that reads JSON from the request body. Which option is syntactically correct?
Attempts:
2 left
💡 Hint
SvelteKit passes an object with a request property to the handler.
✗ Incorrect
The POST handler receives an object with a request property. You must destructure it as { request } and await request.json() to parse the body.
🔧 Debug
advanced2:00remaining
Why does this +server.js GET handler cause a runtime error?
This GET handler throws an error when called. What is the cause?
Svelte
export async function GET() { return { body: { message: 'Hi' } }; }
Attempts:
2 left
💡 Hint
SvelteKit expects a Response or a special object shape from server route handlers.
✗ Incorrect
In SvelteKit +server.js, handlers must return a Response object or use the new Response helpers. Returning a plain object with body causes a runtime error.
❓ state_output
advanced1:30remaining
What is the response body after this POST handler runs?
Given this +server.js POST handler, what JSON does the client receive?
Svelte
export async function POST({ request }) { const { name } = await request.json(); return new Response(JSON.stringify({ greeting: `Hello, ${name}!` }), { headers: { 'Content-Type': 'application/json' } }); }
Attempts:
2 left
💡 Hint
The handler reads 'name' from the JSON body and returns a greeting string.
✗ Incorrect
The handler extracts 'name' from the request JSON and returns a greeting message with that name in JSON format.
🧠 Conceptual
expert2:00remaining
Which statement about +server.js route handlers is TRUE?
Select the correct statement about how +server.js route handlers work in SvelteKit.
Attempts:
2 left
💡 Hint
Think about the expected return type of server route handlers.
✗ Incorrect
SvelteKit expects server route handlers to return Response objects or use the Response helpers. Returning plain objects is not supported and causes errors.