Route Handlers
route.ts files expose HTTP endpoints built on the Web Request/Response APIs.
- Export GET, POST, PUT, PATCH, DELETE functions. GET handlers are not cached by default in Next 15+.
- Return NextResponse.json, plain Response, or a streaming Response (SSE, downloads).
- Dynamic params arrive as a Promise in the second argument.
- req.signal fires when the client disconnects: clean up timers there.
See it in this app: src/app/api/projects, api/projects/[id]/export (CSV), api/stream (SSE)
export async function GET(req: Request) {
const stream = new ReadableStream({ /* enqueue chunks */ });
return new Response(stream, {
headers: { "Content-Type": "text/event-stream" },
});
}