Skip to content

Instantly share code, notes, and snippets.

@MeiyappanKannappa
Last active May 11, 2024 11:21
Show Gist options
  • Select an option

  • Save MeiyappanKannappa/b77a0af4d20e95f96a682072be8efdfd to your computer and use it in GitHub Desktop.

Select an option

Save MeiyappanKannappa/b77a0af4d20e95f96a682072be8efdfd to your computer and use it in GitHub Desktop.
const app = require("express")();
const { httpTransport, emitterFor, CloudEvent, HTTP } = require("cloudevents");
const bodyParser = require("body-parser");
// support parsing of application/json type post data
app.use(bodyParser.json());
app.use(bodyParser.json({ type: "application/*+json" }));
app.post("/", (req, res) => {
const event = HTTP.toEvent({ headers: req.headers, body: req.body });
// body and headers come from an incoming HTTP request, e.g. express.js
// Create an emitter to send events to a receiver
const emit = emitterFor(httpTransport("http://localhost:3000/"));
// Create a new CloudEvent
const ce = new CloudEvent({
source: "/",
type: "event:schedulechange-response",
...event
});
// Send it to the endpoint - encoded as HTTP binary by default
emit(ce);
res.send(ce)
});
app.listen(4000, () => {
console.log("Example app listening on port 4000!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment