Created
September 11, 2015 16:09
-
-
Save elado/bbd09ad4ed03b9660779 to your computer and use it in GitHub Desktop.
Proxy all requests to another server, add Access-Control headers
This file contains 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'), | |
httpProxy = require('http-proxy'); | |
var proxy = httpProxy.createProxyServer({}); | |
var server = http.createServer(function (req, res) { | |
console.log(req.url, req.method); | |
res.setHeader('Access-Control-Allow-Origin', '*'); | |
res.setHeader('Access-Control-Request-Method', '*'); | |
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PUT, DELETE'); | |
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); | |
if (req.method === 'OPTIONS') { | |
res.writeHead(200); | |
res.end(); | |
return; | |
} | |
proxy.web(req, res, { target: 'https://api.something.com' }); | |
}); | |
server.listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment