Skip to content

Instantly share code, notes, and snippets.

@weisjohn
Created August 11, 2016 18:53
Show Gist options
  • Save weisjohn/3f696e6709c5edc33f7ed180bc84c4e0 to your computer and use it in GitHub Desktop.
Save weisjohn/3f696e6709c5edc33f7ed180bc84c4e0 to your computer and use it in GitHub Desktop.
node es6 cors https proxy
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