-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (54 loc) · 1.63 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/************************
* ENV
************************/
const host = "localhost";
const port = 3999;
/************************
* INIT
************************/
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import fetch from "node-fetch";
const app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cors());
/************************
* REQUEST
************************/
app.post('/api/', (req, res) => {
const data = req.body;
console.log("========== Request ==========");
console.log(`endpoint: ${data.endpoint}`);
console.log(`method: ${data.method}`);
console.log(`body: ${JSON.stringify(data.body)}`);
const option = {
method: data.method,
headers: {},
};
if(data.method === "POST") {
option.headers["Content-Type"] = "application/json";
option.body = JSON.stringify(data.body);
}
return fetch(data.endpoint, option)
.then(response => {
console.log("========== Response ==========");
console.log(`url: ${response.url}`);
console.log(`status: ${response.status}`);
console.log(`statusText: ${response.statusText}`);
return response.json();
})
.then(json => {
console.log("========== JSON ==========");
console.log(json);
res.json(json);
})
.catch(error => console.log(`error: ${error}`));
});
/************************
* START SERVER
************************/
app.listen(port, () => console.log(`Starting Server ${host}:${port}`));