-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
54 lines (45 loc) · 1.39 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const express = require("express");
const axios = require("axios");
const https = require("https");
const path = require("path");
const PORT = process.env.PORT || 3000;
const app = express();
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve the html page
app.get("/", (req, res) => res.sendFile(path.join(__dirname, "public", "index.html")));
// API POST route
app.post("/api", (req, res) => {
const { method, endpoint, body, username, password } = req.body;
// Configure the API request
let axiosParams = {
method: method,
url: encodeURI(endpoint)
};
if (process.env.NODE_ENV === "development") {
axiosParams.httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
}
if (username.lenth > 0 && password.lenth > 0) {
axiosParams.auth = {
username: username,
password: password
};
}
if (body.lenth > 0) {
axiosParams.data = JSON.parse(body);
}
// Make the API call
axios(axiosParams)
.then(response => {
console.log("Successful API call.");
res.json(response.data).status(200);
})
.catch(error => {
console.log("Error occurred.");
res.json(error);
});
});
app.listen(PORT, () => console.log(`Listening on port ${PORT}`));