What is the purpose of +server.js files in SvelteKit?
They define server-side routes that handle HTTP requests like GET, POST, PUT, and DELETE. These routes run on the server and can fetch or modify data.
Click to reveal answer
beginner
How do you export a GET handler in a +server.js file?
You export an async function named GET that receives a RequestEvent and returns a Response or data wrapped with json().
Click to reveal answer
beginner
What is the difference between client-side routes and server routes in SvelteKit?
Client-side routes handle UI navigation and rendering in the browser. Server routes handle backend logic like data fetching or processing and run on the server.
Click to reveal answer
intermediate
Why should server routes be used for sensitive operations like database access?
Because server routes run on the server, they keep sensitive data and logic hidden from the user, improving security.
Click to reveal answer
beginner
How can you send JSON data from a +server.js route?
Use the json() helper from @sveltejs/kit to return JSON data with proper headers.
Click to reveal answer
What file name does SvelteKit use for server route handlers?
A+server.js
B+page.svelte
Cserver.js
Droute.js
✗ Incorrect
SvelteKit uses +server.js files to define server-side route handlers.
Which HTTP method handler is NOT typically defined in a +server.js file?
AGET
BDELETE
CPOST
DCONNECT
✗ Incorrect
CONNECT is rarely used and not commonly defined in +server.js files; GET, POST, DELETE are standard.
Where does the code in +server.js run?
AOn the server
BIn the browser
CIn the database
DOn the client device
✗ Incorrect
+server.js code runs on the server, not in the browser or client.
How do you return JSON data from a GET handler in +server.js?
Areturn fetch(data)
Breturn json(data)
Creturn data.toString()
Dreturn new Response(data)
✗ Incorrect
The json() helper formats data with correct headers for JSON responses.
Which import is needed to use the json() helper in +server.js?
Aimport { json } from 'svelte';
Bimport json from 'json';
Cimport { json } from '@sveltejs/kit';
DNo import needed
✗ Incorrect
You must import json from '@sveltejs/kit' to use it in server routes.
Explain how to create a simple GET server route in SvelteKit using +server.js.
Think about the function name and how to send JSON back.
You got /4 concepts.
Describe the difference between client-side routes and server routes in SvelteKit and when to use each.
Consider where the code runs and what it controls.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a +server.js file in a SvelteKit project?
easy
A. To style the application using CSS
B. To define client-side UI components
C. To manage database connections directly in the browser
D. To handle server-side HTTP requests like GET or POST
Solution
Step 1: Understand the role of +server.js
This file is used to write server-side code that responds to HTTP requests.
Step 2: Differentiate client and server roles
Client-side UI components and styling are handled elsewhere, not in +server.js.
Final Answer:
To handle server-side HTTP requests like GET or POST -> Option D
Quick Check:
Server routes = handle HTTP requests [OK]
Hint: Remember: +server.js is for server code only [OK]
Common Mistakes:
Confusing +server.js with client components
Thinking CSS or styling goes in +server.js
Assuming database code runs in the browser
2. Which of the following is the correct way to export a GET handler in a +server.js file?
easy
A. export async function get() { return { body: { message: 'Hello' } }; }
B. export function get() { return { body: { message: 'Hello' } }; }
C. export async function GET() { return { body: { message: 'Hello' } }; }
B. {"status":200,"body":{"success":true,"data":[1,2,3]}}
C. [1, 2, 3]
D. An error because body must be a string
Solution
Step 1: Understand the returned object structure
The handler returns an object with status and body. The body is the JSON data sent to the client.
Step 2: Identify the actual response body content
The client receives the body content serialized as JSON: {"success":true,"data":[1,2,3]}.
Final Answer:
{"success":true,"data":[1,2,3]} -> Option A
Quick Check:
Response body = JSON data object [OK]
Hint: Response body is the JSON object inside returned body [OK]
Common Mistakes:
Confusing the whole return object with the response body
Expecting raw arrays or strings instead of JSON objects
Thinking status code is part of JSON body
4. What is wrong with this +server.js POST handler?
export async function POST(request) {
const data = await request.json();
return { status: 201, body: { received: data } };
}
medium
A. Nothing is wrong; this is correct syntax
B. The request parameter is missing the 'event' wrapper
C. The handler must return a Response object, not a plain object
D. The function should be named post in lowercase
Solution
Step 1: Check handler parameter
The POST handler receives an event object containing request. The code names it request but calls request.json(), which fails because the event object has no json() method; use event.request.json() or destructure { request }.
Step 2: Identify the correct fix
The parameter needs destructuring like POST({ request }) or renaming to event with event.request. The request parameter is missing the 'event' wrapper correctly identifies the missing 'event' wrapper.
Final Answer:
The request parameter is missing the 'event' wrapper -> Option B
Quick Check:
Destructure { request } from event [OK]
Hint: Destructure { request } from event parameter [OK]
Common Mistakes:
Using lowercase method names instead of uppercase
Expecting a Response object instead of plain object
Misunderstanding request parameter structure
5. You want to create a +server.js route that accepts a POST request with JSON data, validates that the JSON has a non-empty name string, and returns a 400 error if invalid. Which code snippet correctly implements this?
hard
A. export async function POST({ request }) {
const data = await request.json();
if (!data.name || typeof data.name !== 'string' || data.name.trim() === '') {
return { status: 400, body: { error: 'Name is required' } };
}
return { status: 200, body: { message: `Hello, ${data.name}` } };
}
B. export async function POST(request) {
const data = await request.json();
if (!data.name) {
throw new Error('Name missing');
}
return new Response(JSON.stringify({ message: `Hello, ${data.name}` }), { status: 200 });
}