Cloud Events - Node.js
The default mode has the app reply to your input events with the output event, which is simplest for demonstrating things working in isolation, but is also the model for working for the Knative Eventing concept.
K_SINK
mode has the app send events to the destination encoded in$K_SINK
, which is useful to demonstrate how folks can synthesize events to send to a Service or Broker when not initiated by a Broker invocation (e.g. implementing an event source)
The application will use $K_SINK
-mode whenever the environment variable is specified.
Follow the steps below to create the sample code and then deploy the app to your cluster. You can also download a working copy of the sample, by running the following commands:
- Docker installed and running on your local machine, and a Docker Hub account configured (we’ll use it for a container registry).
The Sample Code
In the index.js
file, you will see two key functions for the different modes of operation:
const receiveAndSend = (cloudEvent, res) => {
// This is called whenever an event is received if $K_SINK is set, and sends a new event
// to the url in $K_SINK.
const receiveAndReply = (cloudEvent, res) => {
// This is called whenever an event is received if $K_SINK is NOT set, and it replies with
}
In the Dockerfile
, you can see how the dependencies are installed using npm. You can build and push this to your registry of choice via:
docker build -t <image> .
{{< tabs name=”cloudevents_nodejs_deploy” default=”kn” >}} {{% tab name=”yaml” %}}
{{< /tab >}} {{% tab name=”kn” %}}
To deploy using the kn
CLI:
kn service create cloudevents-nodejs --image=<image>
{{ /tab }}{{ /tabs }}
Testing the sample
Get the URL for your Service with:
$ kubectl get ksvc
NAME URL LATESTCREATED LATESTREADY READY REASON
cloudevents-nodejs http://cloudevents-nodejs.default.1.2.3.4.xip.io cloudevents-nodejs-ss5pj cloudevents-nodejs-ss5pj True
Then send a cloud event to it with:
You will get back:
To remove the sample app from your cluster, delete the service.
Run:
{{< /tab >}} {{% tab name=”kn” %}}
Run:
{{ /tab }}{{ /tabs }}
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please .