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
Step 1: Check imports
The code uses NextRequest but does not import it from 'next/server'. This causes a runtime error.
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.
Final Answer:
Missing import of NextRequest -> Option A
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
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.
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.
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.
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
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]