Docs update, added CORS to some responses.

This commit is contained in:
Rob W
2013-01-03 22:34:49 +01:00
parent 8a27a896d4
commit 495a99ebde
3 changed files with 24 additions and 11 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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/