Skip to content

Latest commit

 

History

History
50 lines (37 loc) · 1010 Bytes

README.MD

File metadata and controls

50 lines (37 loc) · 1010 Bytes

CORS Solution using a Proxy Server and Koa

JavaScript

const Koa = require("koa");
const cors = require("@koa/cors");
const proxy = require("koa-proxies");
const app = new Koa();
const port = process.env.PORT || 3000;

app.use(cors());

app.use(
  proxy("/", {
    target: "https://app.mydomain.com",
    changeOrigin: true,
    logs: true,
  })
);

app.listen(port);
console.log(`listening on port ${port}`);

TypeScript

import Koa from "koa";
import cors from "@koa/cors";
import proxy from "koa-proxies";

const app: Koa = new Koa();
const port: number | string = process.env.PORT || 3000;

app.use(cors());

const proxyOptions: proxy.IKoaProxiesOptions = {
  target: "https://app.mydomain.com",
  changeOrigin: true,
  logs: true,
};

app.use(proxy("/", proxyOptions));

app.listen(port);
console.log(`listening on port ${port}`);

Feel free to clone this repository and run both JavaScript and TypeScript versions of the Proxy Server to solve the CORS issue on your side.