What are Route Handlers and how do they replace API routes?
Take a moment.
Say your answer out loud or write it down first — retrieval is what makes it stick. Reveal when you're ready.
Short answer
Route Handlers live in route.ts files and export functions per HTTP method (GET, POST...), replacing pages/api/*. They're full Request/Response Web-standard handlers, cacheable like fetches, and colocated with the routes they serve.
Detailed explanation
app/api/users/route.ts exports async GET(request) { ... } replacing the old default-export switch statements. They use Web standard APIs — Request, Response, URLSearchParams — so code ports to edge runtimes, workers, tests unchanged. Method exports map directly to verbs; unsupported methods fall through to 405 automatically. Caching integrates with the same machinery as fetch: static handlers (no request-time input) can be prerendered, revalidate applies, dynamic = 'force-dynamic' forces runtime. Reading request data via searchParams/headers opts into dynamism. Handlers suit external API surface areas — webhooks, REST endpoints for mobile apps. Important nuance: when both client and server are Next.js, Server Components often call databases/services *directly*, skipping an internal API hop entirely — handlers aren't always needed. Middleware remains separate at the root for cross-cutting concerns.
Example
// app/api/subscribe/route.ts
export async function POST(request: Request) {
const { email } = await request.json();
if (!email?.includes('@')) {
return Response.json({ error: 'Invalid email' }, { status: 400 });
}
await addSubscriber(email);
return Response.json({ ok: true }, { status: 201 });
}
export async function GET() {
const count = await subscriberCount();
return Response.json({ count });
}Interview tip
Show the method-export mapping in code, then drop the senior insight — 'internal calls often skip HTTP entirely with RSC' — it reframes when handlers are actually necessary.
Common mistakes
- Building internal APIs out of habit instead of querying from Server Components.
- Not knowing Web-standard Request/Response powers them.