forked from cloudevents/sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (34 loc) · 930 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* eslint-disable no-console */
const express = require("express");
const { HTTPReceiver } = require("../../");
const app = express();
const receiver = new HTTPReceiver();
app.use((req, res, next) => {
let data = "";
req.setEncoding("utf8");
req.on("data", function(chunk) {
data += chunk;
});
req.on("end", function() {
req.body = data;
next();
});
});
app.post("/", function(req, res) {
console.log(req.headers);
console.log(req.body);
try {
const event = receiver.accept(req.headers, req.body);
const asJSON = event.format();
console.log(`Accepted event: ${JSON.stringify(event.format(), null, 2)}`);
res.status(201).json(asJSON);
} catch (err) {
console.error(err);
res.status(415)
.header("Content-Type", "application/json")
.send(JSON.stringify(err));
}
});
app.listen(3000, function() {
console.log("Example app listening on port 3000!");
});