Skip to content

Commit 5baa52d

Browse files
committed
Fix example body read
Signed-off-by: Fabio José <[email protected]>
1 parent 365e087 commit 5baa52d

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

examples/express-ex/index.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
1-
var express = require('express');
1+
var express = require("express");
22
var app = express();
33

4-
app.get('/', function (req, res) {
4+
const Unmarshaller02 = require("cloudevents-sdk/http/unmarshaller/v02");
5+
var unmarshaller = new Unmarshaller02();
6+
7+
app.use((req, res, next) => {
8+
var data='';
9+
10+
req.setEncoding('utf8');
11+
req.on('data', function(chunk) {
12+
data += chunk;
13+
});
14+
15+
req.on('end', function() {
16+
req.body = data;
17+
next();
18+
});
19+
});
20+
21+
app.post('/', function (req, res) {
522
console.log(req.headers);
623
console.log(req.body);
724

8-
// TODO use the Unmarshaller
9-
10-
res.send('Hello World!');
25+
try {
26+
var event = unmarshaller.unmarshall(req.body, req.headers);
27+
28+
res.status(201)
29+
.send("Event Accepted");
30+
}catch(e) {
31+
console.error(e);
32+
33+
res.status(400)
34+
.header("Content-Type", "application/json")
35+
.send(JSON.stringify(e));
36+
}
1137
});
1238

1339
app.listen(3000, function () {

0 commit comments

Comments
 (0)