-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
38 lines (33 loc) · 1.01 KB
/
server.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
const fastify = require("fastify")({ logger: true });
const path = require("path");
const axios = require("axios");
// proxy jsonrpc to avoid cors issues
const apiKoinos = "http://harbinger-api.koinos.io:8080";
fastify.options("/jsonrpc", async (req, reply) => {
reply.header("Access-Control-Allow-Origin", "*");
reply.header(
"Access-Control-Allow-Headers",
"Origin, Content-Type, X-Auth-Token"
);
reply.header("Access-Control-Allow-Methods", "POST, OPTIONS");
reply.send({});
});
fastify.register(require("fastify-static"), {
root: path.join(__dirname, "3rdpage"),
prefix: "/",
});
fastify.post("/jsonrpc", async (req, reply) => {
const response = await axios.post(apiKoinos, req.body);
reply.header("Access-Control-Allow-Origin", "*");
reply.header("Access-Control-Allow-Methods", "POST, OPTIONS");
reply.send(response.data);
});
const start = async () => {
try {
await fastify.listen(8081, "0.0.0.0");
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();