-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (46 loc) · 1.52 KB
/
index.js
File metadata and controls
55 lines (46 loc) · 1.52 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
const express = require('express');
const morgan = require("morgan");
const cors = require('cors');
const { createProxyMiddleware, responseInterceptor } = require('http-proxy-middleware');
const fetch = require('node-fetch');
// Create Express Server
const app = express();
// Configuration
const PORT = process.env.PORT ? parseInt(process.env.PORT) : 3000;
const HOST = '0.0.0.0';
const API_SERVICE_URL = process.env.PROXY_URL;
const REDIRECT_ERRORS = process.env.REDIRECT_ERRORS;
const onError = (err, req, res, target) => {
console.log("Error: ", err, target);
};
const onProxyRes = responseInterceptor(async (responseBuffer, proxyres, req, res) => {
if (proxyres.statusCode >= 400 && proxyres.statusCode < 500 && REDIRECT_ERRORS) {
const result = await fetch(REDIRECT_ERRORS.match(/^http/) ? REDIRECT_ERRORS : API_SERVICE_URL + REDIRECT_ERRORS);
const headers = result.headers.raw();
for (let key in headers) {
res.setHeader(key, headers[key]);
}
const buf = await result.buffer();
res.statusCode = result.status;
res.statusMessage = result.statusText;
return buf;
} else {
return responseBuffer;
}
});
app.use(morgan('dev'));
app.get(process.env.HEALTH_URI || '/health-check', (req, res, next) => {
res.send('OK');
});
app.use(cors(process.env.CORS_DOMAIN || '*'));
app.use(createProxyMiddleware({
target: API_SERVICE_URL,
changeOrigin: true,
onProxyRes,
onError,
selfHandleResponse: true
}));
// Start the Proxy
app.listen(PORT, HOST, () => {
console.log(`Starting Proxy at ${HOST}:${PORT}`);
});