CORS Proxy

🔬 Testing

Creating a CORS proxy server using testing/RunKit

Table of Contents

Problem

jstestbefore> Response !error
return (async (fn) => {
  try {
    const resp = await fetch("https://example.com");
    console.log(resp.status);
    return resp.text();
  } catch (err) {
    return err.message;
  }
})();

ResponseerrorUpdated 138w ago
Origin https://dotlit.org is not allowed by Access-Control-Allow-Origin.

Implementation

jsrunkit-express-cors-proxy.jssource
const express = require("@runkit/runkit/express-endpoint/1.0.0");
const cors = require("cors");
const request = require("request");

const app = express(exports);
app.use(cors());
app.use("/", (req, res) => {
  try {
    req.pipe(request("https://" + req.url.slice(1))).pipe(res);
    //  res.end(req.url);
  } catch (err) {
    res.end(err.message);
  }
});

Testing & Usage

jsgetendpoint
return (async (fn) => {
  const endpoint = await lit.file.data.plugins.proxy.corsProxy(true);
  return endpoint
})();

txtUpdated 137.9w ago
https://k3vlrljdnj9t.runkit.sh
jstestafter> text !collapse
return (async (fn) => {
  const fetch = await lit.file.data.plugins.proxy.corsProxy();
  const resp = await fetch('https://example.com')
  console.log(resp.status)
  return resp.text();
})();

imgrepl-output.svgUpdated 138w ago
imginline< /testing/repl-output.svg
root nodesome first level nodesecond level nodeanother second level nodeanother first level node
htmlbelow
<img src="/testing/repl-output.svg"/>
js
return lit.fs.readStat("/testing/repl-output.svg", "utf8");

js
return fetch('/testing/repl-output.svg')
       .then(res => res.headers.get('server'))
txtUpdated 139.0w ago
GitHub.com
js
return fetch('/testing/repl-output.svg')
       .then(res => res.headers.get('Content-Type'))
txtUpdated 139.0w ago
image/svg+xml
js> svg
return fetch('/testing/repl-output.svg')
       .then(res => res.text())
svgUpdated 138w ago
root nodesome first level nodesecond level nodeanother second level nodeanother first level node