Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy path option #439

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 2 additions & 0 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -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).',
Expand Down Expand Up @@ -103,6 +104,7 @@ function listen(port) {
ext: argv.e || argv.ext,
logFn: logger.request,
proxy: proxy,
proxyPath: argv.R,
showDotfiles: argv.dotfiles
};

Expand Down
8 changes: 8 additions & 0 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down