From 542ac749df717ba080ca9b7de8ed745563e596cd Mon Sep 17 00:00:00 2001 From: Yannick Galatol Date: Wed, 9 Jun 2021 17:03:34 +0200 Subject: [PATCH] add ability to pass proxyOptions --- README.md | 2 ++ bin/http-server | 23 ++++++++++++++++++++++- lib/http-server.js | 3 ++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eeb3f9868..b1dcc3243 100644 --- a/README.md +++ b/README.md @@ -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] diff --git a/bin/http-server b/bin/http-server index 42ebf651f..3bec1944b 100755 --- a/bin/http-server +++ b/bin/http-server @@ -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', @@ -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, @@ -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 @@ -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'); diff --git a/lib/http-server.js b/lib/http-server.js index 8bafdf8e9..1326bd4c8 100644 --- a/lib/http-server.js +++ b/lib/http-server.js @@ -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,