-
Notifications
You must be signed in to change notification settings - Fork 5
/
proxy.js
75 lines (64 loc) · 1.64 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
64
65
66
67
68
69
70
71
72
73
74
75
const http = require("http");
const httpProxy = require("http-proxy");
const config = require("./config.json");
//
// Create your proxy server and set the target in the options.
//
const proxy = httpProxy.createProxyServer({});
var sendError = function (res, err) {
return res.status(500).send({
error: err,
message: "An error occured in the proxy",
});
};
// error handling
proxy.on("error", function (err, req, res) {
sendError(res, err);
});
var enableCors = function (req, res) {
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");
}
};
// set header for CORS
proxy.on("proxyRes", function (proxyRes, req, res) {
enableCors(req, res);
});
var server = http.createServer(function (req, res) {
// You can define here your custom logic to handle the request
// and then proxy the request.
if (req.method === "OPTIONS") {
enableCors(req, res);
res.writeHead(200);
res.end();
return;
}
proxy.web(
req,
res,
{
target: `https://localhost:9200`,
auth: `${config.user}:${config.password}`,
ssl: false,
secure: false,
changeOrigin: true,
},
function (err) {
sendError(res, err);
}
);
});
server.listen(9220);