Next.js
Route Handlers
Route Handlers are API routes in App Router. They live in app/api/.../route.js and return HTTP responses.
Basics
- Located in
app/api/.../route.js. - Server-only execution; secrets, DB, file system available.
- Export HTTP methods:
GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS.
export async function GET() {
return Response.json({ status: "ok" });
}Routing
app/api/users/route.js→/api/usersapp/api/products/[id]/route.js→/api/products/:id
export function GET(request, { params }) {
return Response.json({ id: params.id });
}Runtime
- Node.js runtime or Edge runtime.
- Can use databases,
cookies(),headers(), streaming responses.
Caching
- Edge routes are cached by CDN unless disabled.
- Node routes are not cached by default.
- Control via
Cache-Controlheaders.
vs Server Actions
- Route Handlers are HTTP endpoints.
- Server Actions are direct server functions without HTTP.