From fc9cf157d2936e57d3c7aecbbe7b4e0f1cb4ccbe Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Fri, 26 Feb 2016 16:21:59 +0100 Subject: [PATCH] Remove getHandler It is not supported, and its current form was not documented. If you need to proxy a request without listening on a port, just dispatch a 'request' event on the return value of createServer(). --- README.md | 15 ++++++--------- lib/cors-anywhere.js | 18 +++++++++--------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 8d333a3..9e87534 100644 --- a/README.md +++ b/README.md @@ -85,13 +85,8 @@ jQuery.ajaxPrefilter(function(options) { ### Server -The module exports two properties: `getHandler` and `createServer`. - -* `getHandler(options)` returns a handler which implements the routing logic. - This handler is used by [http-proxy](https://github.com/nodejitsu/node-http-proxy). -* `createServer(options)` creates a server with the default handler. - -The following options are recognized by both methods: +The module exports `createServer(options)`, which creates a server that handles +proxy requests. The following options are supported: * function `getProxyForUrl` - If set, specifies which intermediate proxy to use for a given URL. If the return value is void, a direct request is sent. The default implementation is @@ -110,9 +105,11 @@ The following options are recognized by both methods: * dictionary of lowercase strings `setHeaders` - Set headers for the request (overwrites existing ones). Example: `{"x-powered-by": "CORS Anywhere"}` -`createServer` recognizes the following option as well: +For advanced users, the following options are also provided. -* `httpProxyOptions` - Options for http-proxy. The documentation for these options can be found [here](https://github.com/nodejitsu/node-http-proxy#options). +* `httpProxyOptions` - Under the hood, [http-proxy](https://github.com/nodejitsu/node-http-proxy) + is used to proxy requests. Use this option if you really need to pass options + to http-proxy. The documentation for these options can be found [here](https://github.com/nodejitsu/node-http-proxy#options). * `httpsOptions` - If set, a `https.Server` will be created. The given options are passed to the [`https.createServer`](https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener) method. diff --git a/lib/cors-anywhere.js b/lib/cors-anywhere.js index 93c3700..57cbb50 100644 --- a/lib/cors-anywhere.js +++ b/lib/cors-anywhere.js @@ -207,7 +207,7 @@ function parseURL(req_url) { } // Request handler factory -var getHandler = exports.getHandler = function(options, proxy) { +function getHandler(options, proxy) { var corsAnywhere = { getProxyForUrl: getProxyForUrl, // Function that specifies the proxy to use maxRedirects: 5, // Maximum number of redirects to be followed. @@ -217,13 +217,13 @@ var getHandler = exports.getHandler = function(options, proxy) { removeHeaders: [], // Strip these request headers. setHeaders: {}, // Set these request headers. }; - if (options) { - Object.keys(corsAnywhere).forEach(function(option) { - if (Object.prototype.hasOwnProperty.call(options, option)) { - corsAnywhere[option] = options[option]; - } - }); - } + + Object.keys(corsAnywhere).forEach(function(option) { + if (Object.prototype.hasOwnProperty.call(options, option)) { + corsAnywhere[option] = options[option]; + } + }); + // Convert corsAnywhere.requireHeader to an array of lowercase header names, or null. if (corsAnywhere.requireHeader) { if (typeof corsAnywhere.requireHeader === 'string') { @@ -321,7 +321,7 @@ var getHandler = exports.getHandler = function(options, proxy) { proxyRequest(req, res, proxy); }; -}; +} // Create server with default and given values // Creator still needs to call .listen()