summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTuomas Siipola2020-01-04 15:16:28 +0200
committerTuomas Siipola2020-01-04 15:16:28 +0200
commitf332157539fe55347aa06cdec780c18aee1b34dc (patch)
tree3f94a460bb5e83294068a9efcef1a5c78988a01d
Initial commitHEADmaster
-rwxr-xr-xcors-proxy35
1 files changed, 35 insertions, 0 deletions
diff --git a/cors-proxy b/cors-proxy
new file mode 100755
index 0000000..7867ebb
--- /dev/null
+++ b/cors-proxy
@@ -0,0 +1,35 @@
+#!/usr/bin/env node
+const http = require('http');
+if (process.argv.length !== 3) {
+ console.log('Usage: cors-proxy url');
+ process.exit(1);
+}
+const log = msg => console.log(`[${new Date().toISOString()}] ${msg}`);
+const server = http.createServer((cltReq, cltRes) => {
+ if (cltReq.method === 'OPTIONS') {
+ cltRes.writeHead(200, {
+ 'Access-Control-Allow-Origin': cltReq.headers['origin'],
+ 'Access-Control-Allow-Methods':
+ cltReq.headers['access-control-request-method'],
+ 'Access-Control-Allow-Headers':
+ cltReq.headers['access-control-request-headers'],
+ });
+ cltRes.end();
+ } else {
+ log(`${cltReq.method} ${cltReq.url}`);
+ const srvReq = http.request(
+ process.argv[2] + cltReq.url,
+ { method: cltReq.method, headers: cltReq.headers },
+ srvRes => {
+ cltRes.writeHead(srvRes.statusCode, {
+ ...srvRes.headers,
+ 'Access-Control-Allow-Origin': cltReq.headers['origin'],
+ });
+ srvRes.pipe(cltRes);
+ }
+ );
+ cltReq.pipe(srvReq);
+ }
+});
+server.listen(8010);
+log('proxy running at http://localhost:8010');