-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (59 loc) · 1.77 KB
/
Copy pathindex.js
File metadata and controls
70 lines (59 loc) · 1.77 KB
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
var http = require("http");
var httpProxy = require("http-proxy");
const port = 6001;
const fail = process.env.FAIL;
var proxy = httpProxy.createProxyServer({
selfHandleResponse: true,
});
function firstCharToLowercase(string) {
return string.charAt(0).toLowerCase() + string.slice(1);
}
function convertKeysToLowerCase(input) {
if (typeof input !== "object" || input === null) return input;
if (Array.isArray(input)) return input.map(convertKeysToLowerCase);
return Object.keys(input).reduce(function (newObj, key) {
let val = input[key];
let newVal = typeof val === "object" ? convertKeysToLowerCase(val) : val;
newObj[firstCharToLowercase(key)] = newVal;
return newObj;
}, {});
}
var server = http.createServer(function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Request-Method", "*");
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET");
res.setHeader("Access-Control-Allow-Headers", "*");
if (req.method === "OPTIONS") {
res.writeHead(200);
res.end();
return;
}
proxy.on("proxyRes", function (proxyRes, req, res) {
var body = [];
proxyRes.on("data", function (chunk) {
body.push(chunk);
});
proxyRes.on("end", function () {
body = Buffer.concat(body).toString();
var bodyJSON;
try {
bodyJSON = JSON.parse(body);
if (fail) {
bodyJSON.failed = true;
}
res.end(JSON.stringify(convertKeysToLowerCase(bodyJSON)));
} catch (e) {
console.error(e);
}
});
});
proxy.web(req, res, {
target: "https://cks.nice.org.uk",
changeOrigin: true,
});
});
console.log(`Proxy listening on port ${port}`);
if (fail) {
console.log("Forcing failed to equal true");
}
server.listen(port);