-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
99 lines (91 loc) · 2.54 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const express = require("express");
const bodyParser = require("body-parser");
const request = require("superagent");
const superagentProxy = require("superagent-proxy");
superagentProxy(request);
const morgan = require("morgan");
const app = express();
const port = 3544;
const http_proxy =
process.env.http_proxy || process.env.https_proxy || undefined;
app.use(express.static("dist"));
app.use(bodyParser.json());
app.use(morgan("combined"));
app.post("/send-test-request", async (req, res) => {
let requestParams = req.body.requestOptions;
let testRequest = request(requestParams.method, requestParams.url);
let headerKeys = Object.keys(requestParams.headers);
for (let headerKey of headerKeys) {
testRequest.set(headerKey, requestParams.headers[headerKey]);
}
if (requestParams.data) {
testRequest.send(requestParams.data);
}
if (http_proxy) {
testRequest.proxy(http_proxy);
}
testRequest.timeout({
response: 15000, // Wait 15s for the server to start sending,
deadline: 25000 // but allow 25s for the file to finish loading.
});
try {
await testRequest;
let testResponse = {
request: {
method: testRequest.method,
url: testRequest.url,
headers: testRequest.header
},
response: {
status: testRequest.response.status,
headers: testRequest.response.header,
body: testRequest.response.body,
text: testRequest.response.text
}
};
res.json(testResponse);
} catch (err) {
// Server response
if (err.response) {
let testResponse = {
request: {
method: err.response.request.method,
url: err.response.request.url,
headers: err.response.request.header
},
response: {
status: err.response.status,
headers: err.response.header,
body: err.response.body,
text: err.response.text
}
};
res.json(testResponse);
} else if (err.code && err.message && err.stack) {
// NodeJS error
res.json({
request: {
method: testRequest.method,
url: testRequest.url,
headers: testRequest.header
},
response: {
error: true,
code: err.code,
text: err.message
}
});
} else {
throw err;
}
}
});
app.use((err, req, res, next) => {
res.status(500).send("Error encountered");
});
app.use((req, res, next) => {
res.status(404).send("Not found");
});
app.listen(port, () => {
process.stdout.write(`Listening on ${port}\n`);
});