0
0
NextJSframework~20 mins

Route handlers (route.ts) in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Route Handler Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the output of this GET route handler?
Consider this Next.js route handler in route.ts. What will the client receive when making a GET request?
NextJS
import { NextResponse } from 'next/server';

export async function GET() {
  return NextResponse.json({ message: 'Hello from GET!' });
}
AA JSON response with {"message": "Hello from GET!"}
BA plain text response with 'Hello from GET!'
CA 404 Not Found error
DA runtime error because NextResponse.json is not a function
Attempts:
2 left
💡 Hint
Look at how NextResponse.json formats the response.
📝 Syntax
intermediate
1:30remaining
Which option correctly defines a POST route handler in route.ts?
You want to create a POST route handler in Next.js using route.ts. Which code snippet is syntactically correct?
Aasync function POST() { return 'Posted'; }
Bexport function post(request: Request) { return new Response('Posted'); }
Cexport async function POST(request: Request) { return new Response('Posted'); }
Dexport async POST(request) => { return new Response('Posted'); }
Attempts:
2 left
💡 Hint
Remember the function must be exported and named exactly POST with async.
state_output
advanced
2:00remaining
What is the response body after this PUT request handler runs?
Given this Next.js PUT route handler, what will the response body contain?
NextJS
import { NextResponse } from 'next/server';

let count = 0;

export async function PUT() {
  count += 1;
  return NextResponse.json({ count });
}
A{"count":0} always, because count resets each request
BA plain text response with the string 'count'
CAn error because count is not defined inside the function
D{"count":1} on first call, increments by 1 each call
Attempts:
2 left
💡 Hint
Consider variable scope and server state persistence between requests.
🔧 Debug
advanced
2:00remaining
Why does this DELETE route handler cause a runtime error?
Examine this DELETE route handler code. Why does it cause a runtime error when called?
NextJS
export async function DELETE() {
  const data = await request.json();
  return new Response('Deleted');
}
AThe variable 'request' is not defined in the function scope
BThe function must not be async for DELETE handlers
CThe Response constructor requires a JSON object, not a string
DMissing import of NextResponse causes runtime error
Attempts:
2 left
💡 Hint
Check the parameters of the function and variable usage.
🧠 Conceptual
expert
2:30remaining
Which statement about Next.js route handlers in route.ts is TRUE?
Select the correct statement about how Next.js route handlers work in route.ts files.
ARoute handlers must use class components to manage state
BRoute handlers run on the server and can access environment variables securely
CRoute handlers automatically cache responses on the client side
DRoute handlers can only export one HTTP method function per file
Attempts:
2 left
💡 Hint
Think about server vs client environment and how Next.js handles route handlers.