Next.js App Router ile API route olusturmak cok kolaylasti. Artik ayri bir backend sunucusuna gerek yok!
GET ve POST Route
// app/api/posts/route.ts - (c) CodeMareFi
import { NextRequest, NextResponse } from 'next/server';
export async function GET() {
const posts = [{ id: 1, title: 'CodeMareFi Post' }];
return NextResponse.json({ posts });
}
export async function POST(req: NextRequest) {
const body = await req.json();
const { title, content } = body;
if (!title || !content) {
return NextResponse.json({ error: 'Eksik alan!' }, { status: 400 });
}
// Veritabanina kaydet...
return NextResponse.json({ success: true, id: Date.now() }, { status: 201 });
}
Dinamik Route
// app/api/posts/[id]/route.ts - (c) CodeMareFi
export async function GET(req: NextRequest, { params }: { params: { id: string } }) {
const { id } = params;
// Veritabanindan getir...
return NextResponse.json({ id, title: 'Post ' + id });
}
© CodeMareFi
Bu icerik codemarefi.com.tr ye aittir.