-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (49 loc) · 1.6 KB
/
index.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
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const {
debugProxyErrorsPlugin, // subscribe to proxy errors to prevent server from crashing
loggerPlugin, // log proxy events to a logger (ie. console)
errorResponsePlugin, // return 5xx response on proxy error
proxyEventsPlugin, // implements the "on:" option
} = require('http-proxy-middleware');
const { possiblyGenerateErrorCode } = require('./generate-http-error-code');
if (!process.argv[1]) {
console.error(`File not found`)
}
const urls = require('./' + process.argv[process.argv.length - 1]);
const { urlsGeneratingErrors, port, target, prefix } = urls;
const app = express();
app.use(
'*',
createProxyMiddleware({
target,
plugins: [debugProxyErrorsPlugin, loggerPlugin, errorResponsePlugin, proxyEventsPlugin],
secure: false,
changeOrigin: true,
logLevel: "debug",
followRedirects: true,
onProxyReq: (proxyReq, req, res) => {
try {
possiblyGenerateErrorCode(req.url, urlsGeneratingErrors)
} catch (err) {
if (err.cause === 'close') {
console.log('Connection closed');
res.socket.destroy();
return;
}
res.status(err.cause).send({
message: err.message
});
}
},
onError: (err, req, res, target) => {
console.log(err);
res.writeHead(500, {
'Content-Type': 'text/plain',
});
res.end('Something went wrong. And we are reporting a custom error message.');
}
}
));
console.log(`Starting server on port ${port}`);
app.listen(port);