0
0
Svelteframework~20 mins

Server routes (+server.js) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server Routes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1: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' }
  });
}
A{"error":"Not found"}
Bundefined
C"Hello from server"
D{"message":"Hello from server"}
Attempts:
2 left
💡 Hint
The GET function returns a Response with JSON string and content-type header.
📝 Syntax
intermediate
1: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?
A
export async function POST(request) {
  const data = await request.json();
  return new Response(JSON.stringify(data));
}
B
export async function POST({ request }) {
  const data = await request.json();
  return new Response(JSON.stringify(data));
}
C
export async function POST(req) {
  const data = req.json();
  return new Response(JSON.stringify(data));
}
D
export async function POST({ req }) {
  const data = await req.json();
  return new Response(JSON.stringify(data));
}
Attempts:
2 left
💡 Hint
SvelteKit passes an object with a request property to the handler.
🔧 Debug
advanced
2: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' }
  };
}
AThe handler must return a Response object, not a plain object with body.
BThe body property must be a string, not an object.
CThe function must be named get, not GET.
DThe handler must be synchronous, not async.
Attempts:
2 left
💡 Hint
SvelteKit expects a Response or a special object shape from server route handlers.
state_output
advanced
1: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' }
  });
}
A{"name":"Alice"}
B{"greeting":"Hello, undefined!"}
C{"greeting":"Hello, Alice!"}
D{"error":"Missing name"}
Attempts:
2 left
💡 Hint
The handler reads 'name' from the JSON body and returns a greeting string.
🧠 Conceptual
expert
2:00remaining
Which statement about +server.js route handlers is TRUE?
Select the correct statement about how +server.js route handlers work in SvelteKit.
AHandlers must always return a Response object; returning plain objects causes errors.
BHandlers can return plain objects with a 'body' property and SvelteKit converts them automatically.
CHandlers can only handle GET and POST methods; other HTTP methods are not supported.
DHandlers run only on the client side and cannot access server resources.
Attempts:
2 left
💡 Hint
Think about the expected return type of server route handlers.