How to use proper HTTP methods in Next.js API routes?
Assesses fundamental understanding of Next.js conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
In Next.js API routes, you can define different functions for each HTTP method (GET, POST, PUT, DELETE) within the same route file. This allows you to handle different types of requests appropriately.
Example:
// app/api/users/route.js
export async function GET(request) {
const users = await getUsers();
return new Response(JSON.stringify(users), {
headers: { "Content-Type": "application/json" },
});
}
export async function POST(request) {
const data = await request.json();
const user = await createUser(data);
return new Response(JSON.stringify(user), {
status: 201,
headers: { "Content-Type": "application/json" },
});
}
export async function PUT(request) {
const data = await request.json();
const user = await updateUser(data);
return new Response(JSON.stringify(user), {
headers: { "Content-Type": "application/json" },
});
}
export async function DELETE(request) {
const { searchParams } = new URL(request.url);
const userId = searchParams.get("id");
await deleteUser(userId);
return new Response(null, { status: 204 });
}
In this example, each function corresponds to a specific HTTP method, allowing you to handle requests accordingly.
Candidate Response Strategy & Interview Tips
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.