Skip to content

Instantly share code, notes, and snippets.

@8bitDesigner
Created June 25, 2014 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 8bitDesigner/11fb7b1329fab6a4e782 to your computer and use it in GitHub Desktop.
Save 8bitDesigner/11fb7b1329fab6a4e782 to your computer and use it in GitHub Desktop.

Usage

node index.js <source server> <destination server> [optional cookie string]

cors-proxy accepts HTTP requests and proxies them to the destination server as though they originated from the source server. This allows you to, for example, develop locally and make CORS requests against a production or staging server.

var http = require('http')
, https = require('follow-redirects').https
, url = require('url')
, _ = require('underscore')
var from = process.argv[2]
, to = process.argv[3]
, cookie = process.argv[4]
function usage() {
console.log("USAGE: node index.js from_url to_url [optional cookie string]");
console.log("Example:");
console.log(" node index.js https://community.fullscreen.net https://engine.fullscreen.net");
process.exit(0)
}
if (process.argv.length < 4) { usage() }
http.createServer(function(req, res) {
var parsed = url.parse(req.url)
, destOrigin = req.headers.origin
, options = {
hostname: url.parse(to).hostname,
port: 443,
path: parsed.path,
method: req.method,
headers: _.extend({}, req.headers, {
host: url.parse(from).hostname,
origin: from,
cookie: (cookie ? cookie : req.headers.cookie)
})
}
https.request(options, function(proxiedRes) {
res.writeHead(proxiedRes.statusCode, _.extend({}, proxiedRes.headers, {
'access-control-allow-origin': destOrigin
}))
proxiedRes.pipe(res)
}).end()
}).listen(8080, function() {
console.log('listening on port 8080')
})
process.on('error', function(err) {
console.log(err)
})
{
"name": "cors-proxy",
"version": "0.0.0",
"description": "Proxies requests from one server to another, inserting false `Origin` and `Accept-Origin` headers",
"main": "index.js",
"author": "Paul Sweeney <paul@8-bitdesign.com>",
"license": "MIT",
"dependencies": {
"follow-redirects": "0.0.3",
"underscore": "^1.6.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment