-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
40 lines (33 loc) · 952 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
const app = require("express")();
const proxy = require("express-http-proxy");
const { target, apiKey, port, whitelist } = require("./config");
const isNodeSynced = require("./lib/is-node-synced");
const cors = require("cors");
function tryParseJSON (rawJson) {
try {
const object = JSON.parse(rawJson);
if (object && typeof object === "object")
return object;
}
catch (e) {}
return false;
};
app.use(cors());
app.get('/health', (req, res) => {
return isNodeSynced()
.then(result => res.send(result))
.catch(() => res.send({ latestBlock: 0, synced: false }))
});
app.use(proxy(target, {
proxyReqBodyDecorator: (bodyContent) => {
const body = tryParseJSON(bodyContent);
if (body && whitelist.includes(body.method)) {
return {
...body,
key: apiKey,
};
}
return "";
},
}));
app.listen(port);