Update Node.js code samples w/ latest CloudEvents (#4865)

This commit updates two of the examples in this repository that use the
Node.js CloudEvents module. A newer version of the module has been released
and the examples have been updated to use it. The newer version allows us
to eliminate a dependency on axios, and simplify the example code.

Signed-off-by: Lance Ball <lball@redhat.com>
This commit is contained in:
Lance Ball 2022-03-31 15:15:44 -04:00 committed by GitHub
parent cc9f98a8c3
commit 10f1c7637c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 4599 additions and 330 deletions

View File

@ -25,8 +25,7 @@ The following example event source emits an event to the sink every second:
```javascript
// File - index.js
const got = require('got');
const { CloudEvent, Emitter, emitterFor } = require('cloudevents');
const { CloudEvent, Emitter, emitterFor, httpTransport } = require('cloudevents');
const K_SINK = process.env['K_SINK'];
K_SINK || logExit('Error: K_SINK Environment variable is not defined');
@ -47,17 +46,7 @@ setInterval(() => {
}, 1000);
// Create a function that can post an event
const emit = emitterFor(event => {
got.post(K_SINK, event)
.then(response => {
console.log('Event posted successfully');
console.log(response.data);
})
.catch(err => {
console.log('Error during event post');
console.error(err);
});
});
const emit = emitterFor(httpTransport(K_SINK));
// Send the CloudEvent any time a Node.js 'cloudevent' event is emitted
Emitter.on('cloudevent', emit);
@ -76,7 +65,7 @@ function registerGracefulExit() {
function logExit(message = 'Exiting...') {
// Handle graceful exit
console.log(message);
process.stdout.write(`${message}\n`);
process.exit();
}
```

View File

@ -1,5 +1,4 @@
const got = require('got');
const { CloudEvent, Emitter, emitterFor } = require('cloudevents');
const { CloudEvent, Emitter, emitterFor, httpTransport } = require('cloudevents');
const K_SINK = process.env['K_SINK'];
K_SINK || logExit('Error: K_SINK Environment variable is not defined');
@ -20,17 +19,7 @@ setInterval(() => {
}, 1000);
// Create a function that can post an event
const emit = emitterFor(event => {
got.post(K_SINK, event)
.then(response => {
console.log('Event posted successfully');
console.log(response.data);
})
.catch(err => {
console.log('Error during event post');
console.error(err);
});
});
const emit = emitterFor(httpTransport(K_SINK));
// Send the CloudEvent any time a Node.js 'cloudevent' event is emitted
Emitter.on('cloudevent', emit);
@ -49,6 +38,6 @@ function registerGracefulExit() {
function logExit(message = 'Exiting...') {
// Handle graceful exit
console.log(message);
process.stdout.write(`${message}\n`);
process.exit();
}

View File

@ -10,7 +10,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"cloudevents": "^4.0.3",
"got": "^11.8.2"
"cloudevents": "^6.0.1"
}
}

View File

@ -15,11 +15,11 @@ limitations under the License.
*/
const express = require('express')
const { CloudEvent, HTTP } = require('cloudevents')
const { CloudEvent, HTTP, emitterFor, httpTransport } = require('cloudevents')
const PORT = process.env.PORT || 8080
const target = process.env.K_SINK
const app = express()
const axios = require('axios').default;
const emit = target ? emitterFor(httpTransport(target)) : null
const main = () => {
app.listen(PORT, function () {
@ -42,23 +42,15 @@ const receiveAndSend = (cloudEvent, res) => {
source: 'https://github.com/knative/docs/code-samples/serving/cloudevents/cloudevents-nodejs',
data
})
const message = HTTP.binary(ce); // Or HTTP.structured(ce))
// Reply back to dispatcher/client as soon as possible
res.status(202).end()
axios({
method: 'post',
url: target,
data: message.body,
headers: message.headers,
})
.then((responseSink) => {
emit(ce).then((responseSink) => {
console.log(`Sent event: ${JSON.stringify(ce, null, 2)}`)
console.log(`K_SINK responded: ${JSON.stringify({ status: responseSink.status, headers: responseSink.headers, data: responseSink.data }, null, 2)}`)
})
.catch(console.error)
}
// receiveAndReply responds with new event
@ -71,7 +63,7 @@ const receiveAndReply = (cloudEvent, res) => {
})
console.log(`Reply event: ${JSON.stringify(ce, null, 2)}`)
const message = HTTP.binary(ce);
const message = HTTP.binary(ce)
res.set(message.headers)
res.status(200).send(message.body)
}
@ -90,7 +82,7 @@ app.use((req, res, next) => {
app.post('/', function (req, res) {
try {
const event = HTTP.toEvent({headers: req.headers, body: req.body})
const event = HTTP.toEvent({ headers: req.headers, body: req.body })
console.log(`Accepted event: ${JSON.stringify(event, null, 2)}`)
target ? receiveAndSend(event, res) : receiveAndReply(event, res)
} catch (err) {

File diff suppressed because it is too large Load Diff

View File

@ -13,9 +13,8 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"axios": "^0.21.2",
"cloudevents": "^4.0.3",
"express": "^4.17.1",
"cloudevents": "^6.0.1",
"express": "^4.17.3",
"nodemon": "^2.0.4"
},
"devDependencies": {