-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (99 loc) · 3.84 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import chalk from 'chalk';
import commandLineArgs from 'command-line-args';
import commandLineUsage from 'command-line-usage';
import http from 'http';
import httpProxy from 'http-proxy';
const description = 'If you want to use the local development environment with the dev backend,\
this will create a proxy so you won\'t run into CORS issues.\
It accepts the following command line parameters: \r\n\
- Port : the port where the proxy will listen \r\n\
- Target : the DEV backend target to contact. \r\n\r\n\
Example: If you set the port to 3000 and target to http://localhost:45680 then\
your actual "resourceBaseUrl" in setting should be http://localhost:3000/api/v1';
const sections = [
{
header: chalk.bold.green("Preflight request proxy"),
content: chalk.white(description),
},
{
header: 'Options',
optionList: [
{
name: 'port',
alias: 'p',
typeLabel: '{underline number}',
description: 'The listened port au reverse proxy. By default port 3000 is used.'
},
{
name: 'target',
alias: 't',
typeLabel: '{underline string}',
description: 'The targeted URL (for exemple your local API, http://127.0.0.1:8001). By default http://127.0.0.1:8000 is used.'
},
{
name: 'help',
description: 'Print this usage guide.'
}
]
},
{
content: 'Project write by Sébastien DOUTRE: {underline https://github.com/barbhackk}'
}
]
const usage = commandLineUsage(sections)
// Define the command line options
const optionDefinitions = [
{ name: "help", alias: "h", type: Boolean, defaultValue: false },
{ name: "port", alias: "p", type: Number, defaultValue: 3000 },
{ name: "target", alias: "t", type: String, defaultValue: "http://127.0.0.1:8000" }
];
// parse command line options
const options = commandLineArgs(optionDefinitions);
if (options.help) {
console.log(usage);
} else {
// Start the proxy
console.log("Start proxy on port", options.port, "for", options.target);
// Create a proxy server with custom application logic
var 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) {
res.setHeader('access-control-allow-origin', '*');
res.setHeader('access-control-allow-methods', 'GET, PUT, PATCH, POST, DELETE');
res.setHeader('access-control-allow-credentials', 'true');
if (req.headers['access-control-request-headers']) {
res.setHeader('access-control-allow-headers', req.headers['access-control-request-headers']);
}
};
// 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') {
console.log(chalk.magenta(req.method + " | ") + chalk.bgCyan.white("Pre-flight request interception : ") + chalk.bold(req.url));
enableCors(req, res);
res.writeHead(204);
res.end();
return;
}
console.log(chalk.magenta(req.method + " | ") + chalk.gray("Redirect to --> ") + chalk.bold(options.target + req.url));
proxy.web(req, res, {
target: options.target
}, function (err) {
sendError(res, err);
});
});
server.listen(options.port);
}