Last active
May 11, 2024 11:21
-
-
Save MeiyappanKannappa/b77a0af4d20e95f96a682072be8efdfd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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