0
0
NextJSframework~20 mins

Why API routes serve backend logic in NextJS - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Route Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why do Next.js API routes run on the server?
In Next.js, API routes are used to handle backend logic. Why do these routes run on the server instead of the client?
ABecause API routes are compiled into static HTML files.
BBecause API routes need to securely access databases and environment variables, which should not be exposed to the client.
CBecause API routes are only used to render UI components.
DBecause API routes are faster when run on the client browser.
Attempts:
2 left
💡 Hint
Think about what kind of data should stay secret and where it should be processed.
component_behavior
intermediate
1:30remaining
What happens when a Next.js API route is called?
When a client calls a Next.js API route, what is the typical behavior of that route?
AIt executes server-side code, processes the request, and sends back a JSON response.
BIt directly updates the client UI without contacting the server.
CIt reloads the entire Next.js page on the client.
DIt compiles new React components on the client.
Attempts:
2 left
💡 Hint
Think about what an API usually does when called.
📝 Syntax
advanced
2:00remaining
Identify the correct Next.js API route handler syntax
Which option shows the correct way to define a Next.js API route handler that returns a JSON message?
Afunction handler(req, res) { res.send('Hello from API') } export default handler
Bexport function handler(req, res) { return { message: 'Hello from API' } }
Cexport default async function handler(req) { res.json({ message: 'Hello' }) }
Dexport default function handler(req, res) { res.status(200).json({ message: 'Hello from API' }) }
Attempts:
2 left
💡 Hint
Check how the response object is used to send JSON and the function export style.
🔧 Debug
advanced
2:00remaining
Why is this Next.js API route not recommended?
Consider this API route code:
export default function handler(req, res) { res.json({ message: 'Hi' }) }

Why is this not the recommended way to write it?
ABecause res.json is called without setting a status code first.
BBecause the function is missing the async keyword.
CBecause res.json is not a function on the response object in Next.js API routes.
DBecause the function does not return a value.
Attempts:
2 left
💡 Hint
Next.js recommends explicitly setting the status code before sending JSON.
state_output
expert
2:30remaining
What is the output of this Next.js API route when called with a POST request?
Given this API route code:
export default async function handler(req, res) {
  if (req.method === 'POST') {
    const data = await new Promise(resolve => setTimeout(() => resolve({ success: true }), 100))
    res.status(201).json(data)
  } else {
    res.status(405).json({ error: 'Method not allowed' })
  }
}

What will the client receive if it sends a POST request?
AAn empty response with HTTP status 200
B{"error":"Method not allowed"} with HTTP status 405
C{"success":true} with HTTP status 201
DA JSON parse error because the response is delayed
Attempts:
2 left
💡 Hint
Look at the method check and the response sent for POST requests.