Couchbase Eventing ile belge ekleme (INSERT), güncelleme (UPDATE) ve silme (DELETE) işlemlerini tespit edip, Node.js API’ye JSON formatında gönderdim.
1️⃣ Node.js API’yi Oluşturma
Öncelikle, Node.js ile bir API yazıp Couchbase’den gelen verileri alacak bir endpoint oluşturuyoruz.
📌 Node.js API Kodu (app.js):
const Express = require("express");
const BodyParser = require("body-parser");
const app = Express();
app.use(BodyParser.json());
app.use(BodyParser.urlencoded({ extended: true }));
// Couchbase Eventing'den gelen verileri yakalayacak endpoint
app.post("/notify", (req, res) => {
console.log("Gelen Veri:", req.body);
res.send("OK");
});
// Sunucuyu başlat
const server = app.listen(3000, () => {
console.log("Node.js API listening on port 3000...");
});
📌 Ne yapıyoruz?
✅ Express.js kullanarak bir REST API oluşturduk.
✅ POST /notify endpoint’i ile Couchbase Eventing’den gelen verileri yakalıyoruz.
✅ Gelen veriyi console.log() ile terminalde görüntülüyoruz.
2️⃣ Node.js API’yi Çalıştırma
Bu dosyayı çalıştırmak için terminalde şu komutları giriyoruz:
npm init -y
npm install express body-parser
node app.js
Eğer başarıyla çalışıyorsa “Node.js API listening on port 3000…” mesajını görmelisin.
3️⃣ Couchbase Eventing Function Yazma
Şimdi Couchbase’de Eventing Function oluşturacağız.
📌 Couchbase Eventing Function Kodu:
function OnInsert(doc, meta) {
sendEventToAPI('INSERT', doc, meta);
}
function OnUpdate(doc, meta) {
sendEventToAPI('UPDATE', doc, meta);
}
function OnDelete(meta) {
sendEventToAPI('DELETE', null, meta);
}
function sendEventToAPI(eventType, doc, meta) {
var url = "http://185.59.73.139:3000/notify"; // Node.js API adresi
var headers = { "Content-Type": "application/json" };
var logMessage = {
eventType: eventType,
documentId: meta.id,
newData: doc ? doc : null
};
try {
var response = curl("POST", url, { headers: headers, body: JSON.stringify(logMessage) });
log("Event gönderildi. API Yanıtı: " + response.body);
} catch (err) {
log("Event gönderilirken hata oluştu: " + err);
}
}
4️⃣ Couchbase Eventing Function’ı Konfigüre Etme
📌 Couchbase Arayüzünde:
1️⃣ Eventing sekmesine gir.
2️⃣ Create Function butonuna tıkla.
3️⃣ Function Name olarak bir isim ver.
4️⃣ Listen To Location → Eventing yapılacak bucket’ı seç.
5️⃣ Bindings sekmesine gel, Alias: curl olarak tanımla.
6️⃣ Kodu yapıştır ve deploy et.
Görsel olarakta bırakıyorum
