diff --git a/README.md b/README.md index 9e87534..97dd5b6 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,8 @@ proxy requests. The following options are supported: Example: `["cookie"]` * dictionary of lowercase strings `setHeaders` - Set headers for the request (overwrites existing ones). Example: `{"x-powered-by": "CORS Anywhere"}` +* string `helpFile` - Set the help file (shown at the homepage). + Example: `"myCustomHelpText.txt"` For advanced users, the following options are also provided. diff --git a/lib/cors-anywhere.js b/lib/cors-anywhere.js index 57cbb50..d745d55 100644 --- a/lib/cors-anywhere.js +++ b/lib/cors-anywhere.js @@ -9,13 +9,13 @@ var url = require('url'); var regexp_tld = require('./regexp-top-level-domain'); var getProxyForUrl = require('proxy-from-env').getProxyForUrl; -var help_file = __dirname + '/help.txt'; -var help_text; -function showUsage(headers, response) { - headers['content-type'] = 'text/plain'; - if (help_text != null) { +var help_text = {}; +function showUsage(help_file, headers, response) { + var isHtml = /\.html$/.test(help_file); + headers['content-type'] = isHtml ? 'text/html' : 'text/plain'; + if (help_text[help_file] != null) { response.writeHead(200, headers); - response.end(help_text); + response.end(help_text[help_file]); } else { require('fs').readFile(help_file, 'utf8', function(err, data) { if (err) { @@ -23,8 +23,8 @@ function showUsage(headers, response) { response.writeHead(500, headers); response.end(); } else { - help_text = data; - showUsage(headers, response); // Recursive call, but since data is a string, the recursion will end + help_text[help_file] = data; + showUsage(help_file, headers, response); // Recursive call, but since data is a string, the recursion will end } }); } @@ -210,12 +210,13 @@ function parseURL(req_url) { function getHandler(options, proxy) { var corsAnywhere = { getProxyForUrl: getProxyForUrl, // Function that specifies the proxy to use - maxRedirects: 5, // Maximum number of redirects to be followed. - originBlacklist: [], // Requests from these origins will be blocked. - originWhitelist: [], // If non-empty, requests not from an origin in this list will be blocked. - requireHeader: null, // Require a header to be set? - removeHeaders: [], // Strip these request headers. - setHeaders: {}, // Set these request headers. + maxRedirects: 5, // Maximum number of redirects to be followed. + originBlacklist: [], // Requests from these origins will be blocked. + originWhitelist: [], // If non-empty, requests not from an origin in this list will be blocked. + requireHeader: null, // Require a header to be set? + removeHeaders: [], // Strip these request headers. + setHeaders: {}, // Set these request headers. + helpFile: __dirname + '/help.txt', }; Object.keys(corsAnywhere).forEach(function(option) { @@ -255,7 +256,7 @@ function getHandler(options, proxy) { if (!location) { // Invalid API call. Show how to correctly use the API - showUsage(cors_headers, res); + showUsage(corsAnywhere.helpFile, cors_headers, res); return; } diff --git a/test/customHelp.html b/test/customHelp.html new file mode 100644 index 0000000..2ced07a --- /dev/null +++ b/test/customHelp.html @@ -0,0 +1,7 @@ + +
+ + +