Created
September 27, 2018 13:59
-
-
Save bfritscher/93455280eb849965fd4e0e7d1b68e5b1 to your computer and use it in GitHub Desktop.
Only for prototyping
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'); | |
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