Created
August 11, 2016 18:53
-
-
Save weisjohn/3f696e6709c5edc33f7ed180bc84c4e0 to your computer and use it in GitHub Desktop.
node es6 cors https proxy
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
const http = require('http'); | |
const https = require('https'); | |
const server = http.createServer(); | |
const host = 'example.com'; | |
server.on('request', (req, res) => { | |
// set cors on the response | |
res.setHeader('Access-Control-Allow-Origin', '*'); | |
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Accept-Language'); | |
// manipulate the headers for SSL | |
req.headers.host = host; | |
// form the options | |
var options = { | |
hostname: host, | |
port: 443, | |
path: req.url, | |
method: req.method, | |
headers: req.headers, | |
}; | |
var request = https.request(options, (response) => { | |
res.writeHead(response.statusCode, response.headers); | |
response.on('data', (d) => { res.write(d); }); | |
response.on('end', (d) => { res.end(); }); | |
}); | |
request.on('error', (e) => { console.error(e); }); | |
request.end(); | |
}); | |
server.listen(4000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment