Bird
Raised Fist0
NextJSframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of a route.ts file in Next.js?
easy
A. To configure database connections
B. To style components using CSS modules
C. To create client-side React components
D. To define server-side code that handles HTTP requests for a specific URL

Solution

  1. Step 1: Understand the role of route.ts

    The route.ts file is used in Next.js to write server-side code that responds to HTTP requests for a specific route.
  2. Step 2: Compare with other options

    Styling, client components, and database configs are handled elsewhere, not in route.ts.
  3. Final Answer:

    To define server-side code that handles HTTP requests for a specific URL -> Option D
  4. Quick Check:

    Route handlers = server code for URLs [OK]
Hint: Route handlers handle server requests per URL [OK]
Common Mistakes:
  • Confusing route.ts with client component files
  • Thinking route.ts is for styling
  • Assuming route.ts manages database directly
2. Which of the following is the correct way to export a GET handler in route.ts?
easy
A. export async function GET(request: NextRequest) { return NextResponse.json({ message: 'Hi' }) }
B. export function Get() { return 'Hello' }
C. export async function get() { return new Response('Hello') }
D. export async function fetch() { return 'Hello' }

Solution

  1. Step 1: Recall Next.js route handler syntax

    Next.js expects exported async functions named exactly by HTTP method in uppercase, e.g., GET, with NextRequest parameter and returning NextResponse.
  2. Step 2: Check each option

    export async function GET(request: NextRequest) { return NextResponse.json({ message: 'Hi' }) } matches correct syntax: async, uppercase GET, parameter, and returns NextResponse. Others have wrong function names, casing, or return types.
  3. Final Answer:

    export async function GET(request: NextRequest) { return NextResponse.json({ message: 'Hi' }) } -> Option A
  4. Quick Check:

    Uppercase method + NextRequest + NextResponse = correct [OK]
Hint: Use uppercase HTTP method and NextRequest param [OK]
Common Mistakes:
  • Using lowercase method names like get instead of GET
  • Missing NextRequest parameter
  • Returning plain string instead of NextResponse
3. Given this route.ts code, what will be the JSON response body when a GET request is made?
import { NextResponse } from 'next/server';

export async function GET() {
  return NextResponse.json({ success: true, data: [1, 2, 3] });
}
medium
A. undefined
B. {"success":true,"data":[1,2,3]}
C. {"error":"Method not allowed"}
D. [1, 2, 3]

Solution

  1. Step 1: Analyze the GET handler return

    The GET function returns NextResponse.json with an object containing success: true and data: [1, 2, 3].
  2. Step 2: Understand JSON response format

    This creates a JSON response with the exact object serialized as a JSON string.
  3. Final Answer:

    {"success":true,"data":[1,2,3]} -> Option B
  4. Quick Check:

    NextResponse.json outputs JSON string [OK]
Hint: NextResponse.json sends exact JSON object [OK]
Common Mistakes:
  • Expecting just the array without wrapping object
  • Confusing error responses with success
  • Thinking response is undefined
4. Identify the error in this route.ts code snippet:
export async function POST(request: NextRequest) {
  const data = await request.json();
  return new Response(JSON.stringify(data));
}

export async function GET() {
  return new Response('Hello');
}
medium
A. Missing import of NextRequest
B. GET function must accept a request parameter
C. POST handler should return NextResponse, not Response
D. No error, code is correct

Solution

  1. Step 1: Check imports

    The code uses NextRequest but does not import it from 'next/server'. This causes a runtime error.
  2. Step 2: Validate other parts

    GET handler can omit request parameter if unused. Returning Response is allowed but NextResponse is preferred; not an error. So main error is missing import.
  3. Final Answer:

    Missing import of NextRequest -> Option A
  4. Quick Check:

    Using NextRequest requires import [OK]
Hint: Always import NextRequest when used [OK]
Common Mistakes:
  • Forgetting to import NextRequest
  • Thinking GET must have request param
  • Confusing Response and NextResponse as errors
5. You want to create a route handler in route.ts that responds to both GET and POST requests. The GET returns a JSON list of users, and the POST adds a user from the request body and returns the updated list. Which code snippet correctly implements this behavior?
hard
A. export async function GET(request) { return new Response(JSON.stringify(['Alice', 'Bob'])); } export async function POST(request) { const data = await request.json(); return new Response(JSON.stringify(['Alice', 'Bob'])); }
B. import { NextRequest } from 'next/server'; const users = []; export function get() { return users; } export function post(request) { users.push(request.body.name); return users; }
C. import { NextRequest, NextResponse } from 'next/server'; let users = ['Alice', 'Bob']; export async function GET() { return NextResponse.json(users); } export async function POST(request: NextRequest) { const newUser = await request.json(); users.push(newUser.name); return NextResponse.json(users); }
D. import { NextResponse } from 'next/server'; export async function GET() { return NextResponse.json(['Alice', 'Bob']); } export async function POST() { return NextResponse.json(['Alice', 'Bob', 'Charlie']); }

Solution

  1. Step 1: Check imports and state management

    import { NextRequest, NextResponse } from 'next/server'; let users = ['Alice', 'Bob']; export async function GET() { return NextResponse.json(users); } export async function POST(request: NextRequest) { const newUser = await request.json(); users.push(newUser.name); return NextResponse.json(users); } correctly imports NextRequest and NextResponse, and uses a mutable users array to store state between calls.
  2. Step 2: Verify GET and POST handlers

    GET returns current users as JSON. POST reads JSON body, adds new user, then returns updated list. This matches requirements.
  3. Step 3: Review other options

    import { NextRequest } from 'next/server'; const users = []; export function get() { return users; } export function post(request) { users.push(request.body.name); return users; } uses lowercase method names and no NextResponse, invalid in Next.js route handlers. export async function GET(request) { return new Response(JSON.stringify(['Alice', 'Bob'])); } export async function POST(request) { const data = await request.json(); return new Response(JSON.stringify(['Alice', 'Bob'])); } returns new Response but does not maintain state. import { NextResponse } from 'next/server'; export async function GET() { return NextResponse.json(['Alice', 'Bob']); } export async function POST() { return NextResponse.json(['Alice', 'Bob', 'Charlie']); } POST does not accept request or update users dynamically.
  4. Final Answer:

    import { NextRequest, NextResponse } from 'next/server'; let users = ['Alice', 'Bob']; export async function GET() { return NextResponse.json(users); } export async function POST(request: NextRequest) { const newUser = await request.json(); users.push(newUser.name); return NextResponse.json(users); } -> Option C
  5. Quick Check:

    Correct imports + state + async handlers = import { NextRequest, NextResponse } from 'next/server'; let users = ['Alice', 'Bob']; export async function GET() { return NextResponse.json(users); } export async function POST(request: NextRequest) { const newUser = await request.json(); users.push(newUser.name); return NextResponse.json(users); } [OK]
Hint: Use async GET/POST with NextRequest, NextResponse, and shared state [OK]
Common Mistakes:
  • Using lowercase method names instead of uppercase
  • Not importing NextRequest or NextResponse
  • Not maintaining state between requests
  • Ignoring async/await for request.json()