Created
May 10, 2017 22:18
-
-
Save elado/523415e2edcc8b1df511db6edb0b4297 to your computer and use it in GitHub Desktop.
Node Proxy from HTTPS to HTTP + CORS
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
// DO NOT USE IN PRODUCTION.. OBVIOUSLY. | |
const http = require('http') | |
const httpProxy = require('http-proxy') | |
const { argv } = require('yargs') | |
const proxy = httpProxy.createProxyServer() | |
proxy.on('error', e => console.log('error', e)) | |
const ORIGIN = 'domain.com' | |
const TARGET = 'https://domain.com' | |
const server = http.createServer((req, res) => { | |
console.log(req.url, req.method) | |
req.on('data', message => console.log(' >>> ', message.toString())) | |
if (req.headers.origin) { | |
res.setHeader('Access-Control-Allow-Origin', req.headers.origin) | |
} | |
req.headers['Origin'] = ORIGIN | |
res.setHeader('Access-Control-Allow-Credentials', 'true') | |
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PUT, DELETE') | |
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, Cache-Control') | |
res.setHeader('Access-Control-Expose-Headers', 'Content-Type, Authorization, Cache-Control') | |
res.setHeader('Access-Control-Max-Age', '86400') | |
if (req.method === 'OPTIONS') { | |
res.writeHead(200) | |
res.end() | |
return | |
} | |
try { | |
proxy.web(req, res, { | |
target: TARGET, | |
secure: true, | |
rejectUnauthorized: false, | |
changeOrigin: true | |
}) | |
} | |
catch (e) { | |
console.log('err', e) | |
res.end('error') | |
} | |
}) | |
const port = +argv.port || +argv.p || 9003 | |
console.log('Running on http://localhost:' + port) | |
server.listen(port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment