From 56f2bb315e86db44450edaaeb7c24c63f2807902 Mon Sep 17 00:00:00 2001 From: Alexey Kucherenko Date: Thu, 9 Aug 2018 13:20:04 +0300 Subject: [PATCH 1/3] pass httpProxyOptions --- 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 27f2de7b1..a68b84d9a 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ This will install `http-server` globally so that it may be run from the command `-P` or `--proxy` Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com +`--proxyOptions` Pass proxy [options](https://github.com/nodejitsu/node-http-proxy#options) using nested dotted objects. e.g.: --proxyOptions.secure false + `-S` or `--ssl` Enable https. `-C` or `--cert` Path to ssl cert file (default: `cert.pem`). diff --git a/bin/http-server b/bin/http-server index 926e0dd75..e0a381a59 100755 --- a/bin/http-server +++ b/bin/http-server @@ -33,6 +33,7 @@ if (argv.h || argv.help) { ' -U --utc Use UTC time format in log messages.', '', ' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com', + ' --proxyOptions Pass options to proxy using nested dotted objects. e.g.: --proxyOptions.secure false', '', ' -S --ssl Enable https.', ' -C --cert Path to ssl cert file (default: cert.pem).', @@ -49,9 +50,23 @@ var port = argv.p || parseInt(process.env.PORT, 10), host = argv.a || '0.0.0.0', ssl = !!argv.S || !!argv.ssl, proxy = argv.P || argv.proxy, + proxyOptions = argv.proxyOptions, utc = argv.U || argv.utc, 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, @@ -103,6 +118,7 @@ function listen(port) { ext: argv.e || argv.ext, logFn: logger.request, proxy: proxy, + proxyOptions: proxyOptions, showDotfiles: argv.dotfiles }; @@ -145,7 +161,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 7e3e06df1..51340ee52 100644 --- a/lib/http-server.js +++ b/lib/http-server.js @@ -106,7 +106,8 @@ function HttpServer(options) { })); if (typeof options.proxy === 'string') { - var proxy = httpProxy.createProxyServer({}); + var httpProxyOptions = options.httpProxyOptions || {}; + var proxy = httpProxy.createProxyServer(httpProxyOptions); before.push(function (req, res) { proxy.web(req, res, { target: options.proxy, From 338681219503c1c9f7f9f95c250617be30d94da1 Mon Sep 17 00:00:00 2001 From: Alexey Kucherenko Date: Thu, 9 Aug 2018 14:33:25 +0300 Subject: [PATCH 2/3] fix typo --- lib/http-server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/http-server.js b/lib/http-server.js index 51340ee52..65e156be9 100644 --- a/lib/http-server.js +++ b/lib/http-server.js @@ -106,8 +106,8 @@ function HttpServer(options) { })); if (typeof options.proxy === 'string') { - var httpProxyOptions = options.httpProxyOptions || {}; - var proxy = httpProxy.createProxyServer(httpProxyOptions); + var proxyOptions = options.proxyOptions || {}; + var proxy = httpProxy.createProxyServer(proxyOptions); before.push(function (req, res) { proxy.web(req, res, { target: options.proxy, From b13e6f426b3e09e3343ef6e899c09c62eb9ea220 Mon Sep 17 00:00:00 2001 From: Jade Michael Thornton Date: Mon, 12 Jul 2021 15:01:36 -0500 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a68b84d9a..b3de5473c 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ This will install `http-server` globally so that it may be run from the command `-P` or `--proxy` Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com -`--proxyOptions` Pass proxy [options](https://github.com/nodejitsu/node-http-proxy#options) using nested dotted objects. e.g.: --proxyOptions.secure false +`--proxyOptions` Pass proxy [options](https://github.com/http-party/node-http-proxy#options) using nested dotted objects. e.g.: --proxyOptions.secure false `-S` or `--ssl` Enable https.