Skip to content

Commit 36d61f1

Browse files
committed
Express examples with Spev 1.0
Signed-off-by: Fabio José <[email protected]>
1 parent 03f8796 commit 36d61f1

File tree

4 files changed

+135
-521
lines changed

4 files changed

+135
-521
lines changed

examples/express-ex/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,89 @@
66
npm start
77
```
88

9+
## Spec v1.0
10+
11+
12+
__A Structured One__
13+
14+
> Payload [example](../payload/v1/structured-event-0.json)
15+
16+
```bash
17+
curl -X POST \
18+
-d'@../payload/v1/structured-event-0.json' \
19+
-H'Content-Type:application/cloudevents+json' \
20+
http://localhost:3000/v1
21+
```
22+
23+
__A Structured One with Extension__
24+
25+
> Payload [example](../payload/v1/structured-event-1.json)
26+
27+
```bash
28+
curl -X POST \
29+
-d'@../payload/v1/structured-event-1.json' \
30+
-H'Content-Type:application/cloudevents+json' \
31+
http://localhost:3000/v1
32+
```
33+
34+
__A Structured One with Base64 Event Data__
35+
36+
> Payload [example](../payload/v1/structured-event-2.json)
37+
38+
```bash
39+
curl -X POST \
40+
-d'@../payload/v1/structured-event-2.json' \
41+
-H'Content-Type:application/cloudevents+json' \
42+
http://localhost:3000/v1
43+
```
44+
45+
__A Binary One__
46+
47+
```bash
48+
curl -X POST \
49+
-d'@../payload/data-0.json' \
50+
-H'Content-Type:application/json' \
51+
-H'ce-specversion:1.0' \
52+
-H'ce-type:com.github.pull.create' \
53+
-H'ce-source:https://github.com/cloudevents/spec/pull/123' \
54+
-H'ce-id:45c83279-c8a1-4db6-a703-b3768db93887' \
55+
-H'ce-time:2019-11-06T11:17:00Z' \
56+
http://localhost:3000/v1/binary
57+
```
58+
59+
__A Binary One with Extension__
60+
61+
```bash
62+
curl -X POST \
63+
-d'@../payload/data-0.json' \
64+
-H'Content-Type:application/json' \
65+
-H'ce-specversion:1.0' \
66+
-H'ce-type:com.github.pull.create' \
67+
-H'ce-source:https://github.com/cloudevents/spec/pull/123' \
68+
-H'ce-id:45c83279-c8a1-4db6-a703-b3768db93887' \
69+
-H'ce-time:2019-11-06T11:17:00Z' \
70+
-H'ce-my-extension:extension value' \
71+
http://localhost:3000/v1/binary
72+
```
73+
74+
__A Binary One with Base 64 Encoding__
75+
76+
```bash
77+
curl -X POST \
78+
-d'@../payload/data-1.txt' \
79+
-H'Content-Type:application/json' \
80+
-H'ce-specversion:1.0' \
81+
-H'ce-type:com.github.pull.create' \
82+
-H'ce-source:https://github.com/cloudevents/spec/pull/123' \
83+
-H'ce-id:45c83279-c8a1-4db6-a703-b3768db93887' \
84+
-H'ce-time:2019-11-06T11:17:00Z' \
85+
http://localhost:3000/v1/binary
86+
```
87+
88+
__A Batch One__
89+
90+
TODO
91+
992
## Spec v0.3
1093

1194
__A Structured One__

examples/express-ex/index.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
var express = require("express");
2-
var app = express();
1+
const express = require("express");
2+
const app = express();
33

44
const v03 = require("cloudevents-sdk/v03");
5-
var unmarshaller03 = new v03.HTTPUnmarshaller();
5+
const unmarshaller03 = new v03.HTTPUnmarshaller();
66

77
const v02 = require("cloudevents-sdk/v02");
8-
var unmarshaller02 = new v02.HTTPUnmarshaller();
8+
const unmarshaller02 = new v02.HTTPUnmarshaller();
9+
10+
const v1 = require("cloudevents-sdk/v1");
11+
const structured1 = new v1.StructuredHTTPReceiver();
12+
const binary1 = new v1.BinaryHTTPReceiver();
913

1014
app.use((req, res, next) => {
11-
var data="";
15+
let data="";
1216

1317
req.setEncoding("utf8");
1418
req.on("data", function(chunk) {
@@ -21,6 +25,48 @@ app.use((req, res, next) => {
2125
});
2226
});
2327

28+
app.post("/v1", function (req, res) {
29+
console.log(req.headers);
30+
console.log(req.body);
31+
32+
try {
33+
let myevent = structured1.parse(req.body, req.headers);
34+
// pretty print
35+
console.log("Accepted event:");
36+
console.log(JSON.stringify(myevent.format(), null, 2));
37+
38+
res.status(201)
39+
.json(myevent.format());
40+
41+
} catch (err) {
42+
console.error(err);
43+
res.status(415)
44+
.header("Content-Type", "application/json")
45+
.send(JSON.stringify(err));
46+
}
47+
});
48+
49+
app.post("/v1/binary", function (req, res) {
50+
console.log(req.headers);
51+
console.log(req.body);
52+
53+
try {
54+
let myevent = binary1.parse(req.body, req.headers);
55+
// pretty print
56+
console.log("Accepted event:");
57+
console.log(JSON.stringify(myevent.format(), null, 2));
58+
59+
res.status(201)
60+
.json(myevent.format());
61+
62+
} catch (err) {
63+
console.error(err);
64+
res.status(415)
65+
.header("Content-Type", "application/json")
66+
.send(JSON.stringify(err));
67+
}
68+
});
69+
2470
app.post("/v03", function (req, res) {
2571
console.log(req.headers);
2672
console.log(req.body);

0 commit comments

Comments
 (0)