diff --git a/README.md b/README.md index 313b4e0..c503b1a 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,19 @@ Heroku can be found at https://devcenter.heroku.com/articles/nodejs. ## Example ```javascript -var host = '127.0.0.1'; -var port = 8080; +// Heroku defines the environment variable PORT, and requires the binding address to be 0.0.0.0 +var host = process.env.PORT ? '0.0.0.0' : '127.0.0.1'; +var port = process.env.PORT || 8080; var cors_proxy = require("cors-anywhere"); cors_proxy.createServer({ - xRequestedWith: true + requireHeader: 'x-requested-with', + withCredentials: false, + removeHeaders: ['cookie', 'cookie2'] }).listen(port, host, function() { console.log('Running CORS Anywhere on ' + host + ':' + port); }); + ``` Request examples: @@ -32,6 +36,10 @@ Request examples: * http://localhost:8080/ - Shows usage text, as defined in `libs/help.txt` * http://localhost:8080/favicon.ico - Replies 404 Not found +Live examples: + +* http://cors-anywhere.herokuapp.com/ +* http://rob.lekensteyn.nl/cors-anywhere.html ## Documentation diff --git a/lib/cors-anywhere.js b/lib/cors-anywhere.js index 6c7a9b8..589de4b 100644 --- a/lib/cors-anywhere.js +++ b/lib/cors-anywhere.js @@ -7,19 +7,20 @@ var regexp_tld = require('./regexp-top-level-domain'); var help_file = __dirname + '/help.txt'; var help_text; -function showUsage(res) { +function showUsage(headers, response) { + headers['content-type'] = 'text/plain'; if (help_text != null) { - res.writeHead(200, {'content-type': 'text/plain'}); - res.end(help_text); + response.writeHead(200, headers); + response.end(help_text); } else { require('fs').readFile(help_file, 'utf8', function(err, data) { if (err) { console.error(err); - res.writeHead(500, {}); - res.end(); + response.writeHead(500, headers); + response.end(); } else { help_text = data; - showUsage(res); // Recursive call, but since data is a string, the recursion will end + showUsage(headers, response); // Recursive call, but since data is a string, the recursion will end } }); } @@ -92,7 +93,7 @@ var getHandler = exports.getHandler = function(options) { // 2:host if (!match || (match[2].indexOf('.') === -1 && match[2].indexOf(':') === -1) || match[4] > 65535) { // Incorrect usage. Show how to do it correctly. - showUsage(res); + showUsage(cors_headers, res); return; } else if (match[2] === 'iscorsneeded') { // Is CORS needed? This path is provided so that API consumers can test whether it's necessary @@ -112,7 +113,7 @@ var getHandler = exports.getHandler = function(options) { res.end(); return; } else if (corsAnywhere.requireHeader != null && req.headers[corsAnywhere.requireHeader.toLowerCase()] == null) { - res.writeHead(400, {'Content-Type': 'text/plain'}); + res.writeHead(400, cors_headers); res.end('Missing ' + corsAnywhere.requireHeader + ' header!'); return; } else { diff --git a/lib/help.txt b/lib/help.txt index 5a95a50..49edfd3 100644 --- a/lib/help.txt +++ b/lib/help.txt @@ -1,3 +1,5 @@ +This API enables cross-origin requests to anywhere. + Usage: / Shows help @@ -6,3 +8,5 @@ Usage: The protocol can be omitted. It defaults to http:, unless port 443 is specified. +Demo : http://rob.lekensteyn.nl/cors-anywhere.html +Source code : https://github.com/Rob--W/cors-anywhere/