-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
63 lines (51 loc) · 1.32 KB
/
proxy.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
const QA_SERVER = "https://eu1.locationiq.com/";
const http = require("http");
const httpProxy = require("http-proxy");
const Express = require("express");
const app = new Express();
const server = new http.Server(app);
const proxy = httpProxy.createProxyServer({});
const enableCors = function(req, res, next) {
if (req.headers["access-control-request-method"]) {
res.setHeader(
"access-control-allow-methods",
req.headers["access-control-request-method"]
);
}
if (req.headers["access-control-request-headers"]) {
res.setHeader(
"access-control-allow-headers",
req.headers["access-control-request-headers"]
);
}
if (req.headers.origin) {
res.setHeader("access-control-allow-origin", req.headers.origin);
res.setHeader("access-control-allow-credentials", "true");
}
next();
};
const sendError = (res, error) => console.error(res, error);
app.use((req, res, next) => {
enableCors(req, res, next);
});
app.use(function(req, res, next) {
if (req.method === "OPTIONS") {
res.writeHead(200);
res.end();
return next();
}
proxy.web(
req,
res,
{
target: `${QA_SERVER}`,
secure: true,
changeOrigin: true
},
function(err) {
sendError(res, err);
}
);
});
console.log("listening on port 5050");
server.listen(5050);