Skip to content

Instantly share code, notes, and snippets.

@bfritscher
Created September 27, 2018 13:59
Show Gist options
  • Save bfritscher/93455280eb849965fd4e0e7d1b68e5b1 to your computer and use it in GitHub Desktop.
Save bfritscher/93455280eb849965fd4e0e7d1b68e5b1 to your computer and use it in GitHub Desktop.
Only for prototyping
var http = require('http');
var request = require('request');
function onRequest(req, res){
// Set CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
if ( req.method === 'OPTIONS' ) {
res.writeHead(200);
res.end();
return;
}
var url = 'https://mcd.iosphe.re/' + req.url;
if (url.indexOf('?') < 0){
url += '?';
}else{
url += '&';
}
var options = {
uri: url,
method: req.method
};
console.log(req.method);
if (req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString(); // convert Buffer to string
});
req.on('end', () => {
options.json = JSON.parse(body);
request(options).pipe(res);
});
} else {
console.log(JSON.stringify(options));
request(options).pipe(res);
}
}
http.createServer(onRequest).listen(3051);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment