app/.../route.js using Web Request/Response.
Skip to solutionKEEP THE
mediumFrontend
What are Route Handlers in Next.js and how do they differ from Pages API routes?
496 views
01
Understand the problem
nextjsroute-handlersapiapp-router
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Route Handlers (route.js in the App Router) define HTTP endpoints with named exports (GET, POST, …) using the Web Request/Response APIs. Unlike Pages API routes (req/res Node objects), they're Web-standard, run on Node or Edge, and live in the app/ tree.
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
GET + POST handler
// app/api/todos/route.ts
import { NextResponse } from 'next/server';
export async function GET() {
return NextResponse.json(await db.todo.findMany());
}
export async function POST(request: Request) {
const body = await request.json();
const todo = await db.todo.create({ data: body });
return NextResponse.json(todo, { status: 201 });
}05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 56 of 95 decoded in the Next.js track. One more won't hurt.