summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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');