Created
September 16, 2013 00:38
-
-
Save geelen/6575567 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var http = require('http'), | |
| request = require('request'); | |
| var prodServer = process.env.PROD_SERVER | |
| var target = process.env.TARGET | |
| var app = http.createServer(function (req, resp) { | |
| var origin; | |
| if (req.headers.origin.match(/(localhost|\.dev|\.local)\:\d+$/)) { | |
| origin = req.headers.origin; | |
| } else { | |
| origin = prodServer; | |
| } | |
| resp.setHeader("Access-Control-Allow-Origin", origin); | |
| resp.setHeader("Access-Control-Allow-Headers", "X-Requested-With,Authorization,Content-Type"); | |
| if (req.method === "OPTIONS") { | |
| resp.statusCode = 200; | |
| resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS"); | |
| resp.end() | |
| } else { | |
| req.headers.host = target; | |
| req.pipe(request("https://" + target + req.url)).pipe(resp); | |
| } | |
| }); | |
| var port = process.env.PORT || 5001; | |
| app.listen(port, function () { | |
| console.log("Listening on " + port); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment