Comments API Projesi
Ders 24: Comments API Projesi
Section titled “Ders 24: Comments API Projesi”Öğrenme Hedefleri
Section titled “Öğrenme Hedefleri”- Tam kapsamlı API geliştirme
- Hono ile D1 entegrasyonu
- CRUD operations
İçerik
Section titled “İçerik”Proje Kurulumu
Section titled “Proje Kurulumu”npm create cloudflare@latest comments-apicd comments-apinpm install honoAPI Endpoints
Section titled “API Endpoints”const app = new Hono<{ Bindings: Env }>();
// GET /api/posts/:slug/commentsapp.get('/api/posts/:slug/comments', async (c) => { const slug = c.req.param('slug'); const { results } = await c.env.DB .prepare('SELECT * FROM comments WHERE post_slug = ?') .bind(slug) .run(); return c.json(results);});
// POST /api/posts/:slug/commentsapp.post('/api/posts/:slug/comments', async (c) => { const slug = c.req.param('slug'); const { author, body } = await c.req.json();
const { success } = await c.env.DB .prepare('INSERT INTO comments (author, body, post_slug) VALUES (?, ?, ?)') .bind(author, body, slug) .run();
return c.json({ success }, success ? 201 : 500);});✅ Tam kapsamlı API ✅ CRUD operations
Sonraki Ders
Section titled “Sonraki Ders”Staff Directory Uygulaması - İkinci proje.
Ders Süresi: 90 dakika Zorluk Seviyesi: İleri