Thursday, 12 April, 2018 UTC


Summary

The Couchbase Eventing service is new as of Couchbase 5.5 and I’d previously written a tutorial about detecting sensitive information and sending notifications. In my previous tutorial I demonstrated to how make requests to a remote service using the cURL operator in N1QL. However, what if you didn’t want to use N1QL to get the job done?
We’re going to see how to use the cURL function to make requests rather than N1QL when using the Eventing service.
Rather than trying to find an API to use, we’re going to create our own with Node.js, similar to what we did in the previous example. Once we have the API in place, we’re going to create a function in Couchbase that will trigger when new documents are created or changed.
Creating a Node.js API for Receiving Requests
Our API is going to be incredibly simple because Node.js is not the focus of this tutorial. We’re going to receive data and return that same data back to our function.
Create a new directory on your computer and execute the following commands from the CLI within that directory:
npm init -y
npm install express body-parser --save
The above commands will create a new package.json file and install Express Framework and a module for accepting request bodies. With the dependencies available, execute the following:
touch app.js
If you’re on Windows, go ahead and create the app.js file however you want.
Open the newly created app.js file and include the following JavaScript code:
const Express = require("express");
const BodyParser = require("body-parser");

var app = Express();

app.use(BodyParser.json());
app.use(BodyParser.urlencoded({ extended: true }));

app.post("/notify", (request, response) => {
    console.log("POST /notify");
    response.send(request.body);
});

var server = app.listen(3000, () => {
    console.log("Listening...");
});
We have a single endpoint that does exactly as I previously mentioned. When we run this application, it is serving on port 3000. For our example, both the database and the Node.js application will be running locally. If things are different, make sure the Node.js application is accessible from a remote host.
Developing a Simple Couchbase Function with cURL Support
Our function will be very simple for this example. We won’t be checking for certain data, we’ll only be checking to see if something was created or changed. If the function triggers, we’ll post the data to our Node.js application.
Before we can do that, we need to set up our cURL whitelist. In the Settings section of the Couchbase Administrative Dashboard, add any URL that you wish to access.
If the URL is not added, you’ll get an error when you try to access it.
With the proper whitelist in place, we can create our function. In the Eventing section of the Couchbase Administrative Dashboard, create a new function, mapping the correct Bucket and meta information Bucket. The code we want to add looks like the following:
function OnUpdate(doc, meta) {
    log('document', doc);
    var response = curl("http://localhost:3000/notify", { method: "POST", data: doc });
    log('curl', response);
}
function OnDelete(meta) {
}
Notice that we’re using the curl function and specifying some options such as the request body, and the method. If we wanted to we could add other properties such as headers or authorization.
Try adding a new document or changing one. The document should send.
Conclusion
You just saw how to send data to a remote service from a Couchbase Function with the cURL function and the Couchbase Eventing service. This is a little different than my previous example because it doesn’t use N1QL to make this possible.
To learn more about the Couchbase Eventing service, check out the Couchbase Developer Portal.
The post Using the cURL Function with the Couchbase Eventing Service appeared first on The Couchbase Blog.