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

add ability to pass proxyOptions #459

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
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

`--proxyOptions` Pass proxy [options](https://github.com/nodejitsu/node-http-proxy#options) using nested dotted objects. e.g.: --proxyOptions.secure false
thornjad marked this conversation as resolved.
Show resolved Hide resolved
thornjad marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that there are/have been several issues and pull requests that will be solved by this, I think it's worth adding more to this, especially .secure false, which solves errors around self-signed certs (#214)


`-S` or `--ssl` Enable https.

`-C` or `--cert` Path to ssl cert file (default: `cert.pem`).
Expand Down
23 changes: 22 additions & 1 deletion bin/http-server
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',
' --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).',
Expand All @@ -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 = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the underlying library handle it's own validation, and we just surface any errors? I'd like to avoid duplicating other people's requirements (since they're likely to change over time).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can pass stuff through without this boolean checking, so long as we handle errors from http-proxy in a user-friendly way.

'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 @@ -103,6 +118,7 @@ function listen(port) {
ext: argv.e || argv.ext,
logFn: logger.request,
proxy: proxy,
proxyOptions: proxyOptions,
showDotfiles: argv.dotfiles
};

Expand Down Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion lib/http-server.js
Expand Up @@ -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,
Expand Down