Skip to content

Latest commit

 

History

History
54 lines (40 loc) · 1.36 KB

cloudevents.md

File metadata and controls

54 lines (40 loc) · 1.36 KB

CloudEvents

This guide shows you how to use the Functions Framework for local testing with:

Local Testing of CloudEvents

Install @google-cloud/functions-framework and list it as a dependency in your package.json:

npm install --save @google-cloud/functions-framework

Create an index.js file and declare your function:

const functions = require('@google-cloud/functions-framework');

functions.cloudEvent('helloCloudEvents', (cloudevent) => {
  console.log(cloudevent.specversion);
  console.log(cloudevent.type);
  console.log(cloudevent.source);
  console.log(cloudevent.subject);
  console.log(cloudevent.id);
  console.log(cloudevent.time);
  console.log(cloudevent.datacontenttype);
});

Add a package.json script to start the Functions Framework and pass the name of your function as the target.

{
  "scripts": {
    "start": "functions-framework --target=helloCloudEvents"
  }
}

Start the Functions Framework:

npm start

Your function will be serving at http://localhost:8080/, however, it is no longer accessible via HTTP GET requests from the browser.

Create and send a cloudevent to the function

cloudevents send http://localhost:8080 --specver--id abc-123 --source cloudevents.conformance.tool --type foo.bar