Загрузка…
Загрузка…
route.ts — Web Request/Response, async params в Next 15, JSON, streaming ReadableStream, cookies на response.
// app/api/posts/[id]/route.ts
import { NextRequest, NextResponse } from 'next/server';
export async function GET(
req: NextRequest,
{ params }: { params: Promise },
) {
const { id } = await params; // Next 15: async params
const q = req.nextUrl.searchParams.get('fields'); // query string
const post = await db.post.find(id);
if (!post) return new NextResponse('Not found', { status: 404 });
return NextResponse.json(post);
}// Streaming a response with a ReadableStream (e.g. LLM tokens, SSE)
export async function GET() {
const stream = new ReadableStream({
async start(controller) {
for (const chunk of await source()) controller.enqueue(chunk);
controller.close();
},
});
return new Response(stream, { headers: { 'Content-Type': 'text/event-stream' } });
}// Setting cookies on the response
export async function POST() {
const res = NextResponse.json({ ok: true });
res.cookies.set('session', token, { httpOnly: true, secure: true, sameSite: 'lax' });
return res;
}Route handlers = typed HTTP methods + Web standards; streaming и cookies — те же primitives, что на Edge.
