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.
- A Kubernetes cluster with Knative installed and DNS configured. Follow the installation instructions if you need to create one.
- installed and running on your local machine, and a Docker Hub account configured (we’ll use it for a container registry).
If you look in
index.js
, you will see two key functions for the different modes of operation:// 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
// the new event instead.
}
If you look in
Dockerfile
, you will see how the dependencies are installed using npm. You can build and push this to your registry of choice via:
Then send a cloud event to it with:
$ curl -X POST \
-H "content-type: application/json" \
-H "ce-specversion: 1.0" \
-H "ce-source: curl-command" \
-H "ce-type: curl.demo" \
-H "ce-id: 123-abc" \
-d '{"name":"Dave"}' \
http://cloudevents-nodejs.default.1.2.3.4.xip.io
You will get back:
To remove the sample app from your cluster, delete the service record: