Skip to content

Commit

Permalink
add ability to pass proxyOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickglt committed Jun 9, 2021
1 parent eeb6fce commit 542ac74
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -70,6 +70,8 @@ Using `npx` you can run the script without installing it first:

`-P` or `--proxy` Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com

`--proxy-options` Pass proxy [options](https://github.com/http-party/node-http-proxy#options) using nested dotted objects. e.g.: --proxyOptions.secure false

`--username` Username for basic authentication [none]

`--password` Password for basic authentication [none]
Expand Down
23 changes: 22 additions & 1 deletion bin/http-server
Expand Up @@ -39,6 +39,7 @@ if (argv.h || argv.help) {
' --log-ip Enable logging of the client\'s IP address',
'',
' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com',
' --proxy-options Pass options to proxy using nested dotted objects. e.g.: --proxy-options.secure false',
'',
' --username Username for basic authentication [none]',
' Can also be specified with the env variable NODE_HTTP_SERVER_USERNAME',
Expand All @@ -61,10 +62,24 @@ var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
host = argv.a || '0.0.0.0',
ssl = argv.S || argv.ssl,
proxy = argv.P || argv.proxy,
proxyOptions = argv['proxy-options'],
utc = argv.U || argv.utc,
version = argv.v || argv.version,
logger;

var proxyOptionsBooleanProps = [
'ws', 'xfwd', 'secure', 'toProxy', 'prependPath', 'ignorePath', 'changeOrigin',
'preserveHeaderKeyCase', 'followRedirects', 'selfHandleResponse'
];

if (proxyOptions) {
Object.keys(proxyOptions).forEach(function (key) {
if (proxyOptionsBooleanProps.indexOf(key) > -1) {
proxyOptions[key] = proxyOptions[key] === 'true';
}
});
}

if (!argv.s && !argv.silent) {
logger = {
info: console.log,
Expand Down Expand Up @@ -126,6 +141,7 @@ function listen(port) {
ext: argv.e || argv.ext,
logFn: logger.request,
proxy: proxy,
proxyOptions: proxyOptions,
showDotfiles: argv.dotfiles,
username: argv.username || process.env.NODE_HTTP_SERVER_USERNAME,
password: argv.password || process.env.NODE_HTTP_SERVER_PASSWORD
Expand Down Expand Up @@ -184,7 +200,12 @@ function listen(port) {
}

if (typeof proxy === 'string') {
logger.info('Unhandled requests will be served from: ' + proxy);
if (proxyOptions) {
logger.info('Unhandled requests will be served from: ' + proxy + '. Options: ' + JSON.stringify(proxyOptions));
}
else {
logger.info('Unhandled requests will be served from: ' + proxy);
}
}

logger.info('Hit CTRL-C to stop the server');
Expand Down
3 changes: 2 additions & 1 deletion lib/http-server.js
Expand Up @@ -163,7 +163,8 @@ function HttpServer(options) {
}));

if (typeof options.proxy === 'string') {
var proxy = httpProxy.createProxyServer({});
var proxyOptions = options.proxyOptions || {};
var proxy = httpProxy.createProxyServer(proxyOptions);
before.push(function (req, res) {
proxy.web(req, res, {
target: options.proxy,
Expand Down

0 comments on commit 542ac74

Please sign in to comment.