Express API
- Node.js kursu
- Orta seviye
- Ders 113/116
- 6 dk
Express ile REST API iskeletini kurarsın.
Kullanıcı rotaları
Bu derste /users kaynağı için iki rota ekleyeceğiz: GET /users kullanıcı listesini döndürür, POST /users yeni kullanıcı oluşturur. Express'te app.get ve app.post ayrı yöntemlerdir; aynı yol için farklı işleyiciler kaydeder.
İşleyicilerde res.json(veri) yanıtı JSON olarak gönderir. GET /users boş bir dizi [] döndürebilir; POST /users {created: true} döndürebilir. Her rotayı routes.push(...) ile dizine de eklersin. Sonra routes.map(r => r.method + ' ' + r.path).join(', ') ile rotaları tek dize olarak yazdırırsın. Bu, API yüzeyini özetlemenin temiz yoludur.
Rotayı yazdırma
Tüm rotaları tek bir dize olarak yazdırmak için routes.map(...).join(', ') kullanırsın. map her rotayı bir dizeye çevirir; join bunları virgülle birleştirir. Bu, API yüzeyini özetler.
Örnek: routes.map(r => r.method + ' ' + r.path).join(', '). Üç rota varsa çıktı GET /health, GET /users, POST /users olur. join(', ') ile öğeler arasına virgül ve boşluk konur. Bu, rotaların program içinde denetlenmesinin pratik yoludur. console.log('Rotalar: ' + ...) ile öne etiket eklersin.
Uygulama Kullanıcı rotalarını ekle
app.get('/users', (req, res) => res.json([])) ekle. routes.push({method: 'GET', path: '/users'}) ile kaydet. app.post('/users', (req, res) => res.json({created: true})) ekle. routes.push({method: 'POST', path: '/users'}) ile kaydet. console.log('Rotalar: ' + routes.map(r => r.method + ' ' + r.path).join(', ')) yazdır.
Başlangıç kodu
const express = require('express');
const app = express();
const routes = [];
app.get('/health', (req, res) => res.json({ok: true}));
routes.push({method: 'GET', path: '/health'});
console.log('Sunucu hazır');
// GET /users ve POST /users rotalarını ekle
// rotaları yazdır
// buraya yazÖrnek çözüm
const express = require('express');
const app = express();
const routes = [];
app.get('/health', (req, res) => res.json({ok: true}));
routes.push({method: 'GET', path: '/health'});
console.log('Sunucu hazır');
app.get('/users', (req, res) => res.json([]));
routes.push({method: 'GET', path: '/users'});
app.post('/users', (req, res) => res.json({created: true}));
routes.push({method: 'POST', path: '/users'});
console.log('Rotalar: ' + routes.map(r => r.method + ' ' + r.path).join(', '));Beklenen çıktı
Sunucu hazır Rotalar: GET /health, GET /users, POST /users
Soru ve cevaplar
Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.
1 GET rotasını tanımla
app.____('/users', (req, res) => res.json([]));
app.get('/users', (req, res) => res.json([]));
Açıklama: app.get bir GET rotası kaydeder.
2 Parçayı rolüne eşle
| Kavram | Anlamı |
|---|---|
app.get |
GET rotası kaydeder |
app.post |
POST rotası kaydeder |
res.json |
JSON yanıtı gönderir |
.map |
Her öğeyi dönüştürür |
3 routes.map(r => r.method + ' ' + r.path) ne üretir?
routes.map(r => r.method + ' ' + r.path)- Yol dizesi dizisi Doğru cevap
- Bir sayı dizisi
- Boole dizisi
- Bir promise
Açıklama: map her rotayı 'METHOD path' dizesine çevirir.