luixaviles / cors-proxy-server-koa

Solving the CORS issue using a Proxy Server with Koa

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

Solving the CORS issue using a Proxy Server with Koa


Languages

Language:TypeScript 53.1%Language:JavaScript 46.9%