diff --git a/README.md b/README.md index 74d57a6c..5bf8844a 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 +`-R` Path for which proxy is enabled. e.g.: -U /api/ + `-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 926e0dd7..748a0747 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', + ' -R Path for which proxy is enabled. e.g.: /api/ [none]', '', ' -S --ssl Enable https.', ' -C --cert Path to ssl cert file (default: cert.pem).', @@ -103,6 +104,7 @@ function listen(port) { ext: argv.e || argv.ext, logFn: logger.request, proxy: proxy, + proxyPath: argv.R, showDotfiles: argv.dotfiles }; diff --git a/lib/http-server.js b/lib/http-server.js index 7e3e06df..7ff4eea7 100644 --- a/lib/http-server.js +++ b/lib/http-server.js @@ -107,7 +107,15 @@ function HttpServer(options) { if (typeof options.proxy === 'string') { var proxy = httpProxy.createProxyServer({}); + before.push(function (req, res) { + var shouldNotProxy = + options.proxyPath && req.url.substring(0, options.proxyPath.length) !== options.proxyPath; + + if (shouldNotProxy) { + return; + } + proxy.web(req, res, { target: options.proxy, changeOrigin: true