title: how to allow Access-Control-Allow-Origin with koa
body: <p>I'm new to Koa and i encountered a problem that I didnt get when i used epxress.</p>
  
  <p>here is my code : </p>
  
  <pre><code>const koa = require("koa");
  const koaRouter = require("koa-router");
  const app = new koa();
  const router = new koaRouter();
  const cors = require('koa-cors');
  const bodyParser = require("body-parser");
  
  const port = 8080;
  
  app.use(router.routes()).use(router.allowedMethods());
  app.use(bodyParser.urlencoded({extended: false}));
  app.use(bodyParser.json());
  
  app.listen(port, () =&gt; {
    console.log("server koa started " + port);
  });
  
  router.get("/id/:id", async (ctx) =&gt; {
    const id = ctx.params.id;
    console.log("id was " + id);
    ctx.body = {
      "id": id
    }
  });
  
  router.get("/", async (ctx) =&gt; {
    ctx.body = {
      "message": "hello"
    }
  });
  </code></pre>
  
  <p>when I want to call this from my webserver i got this :</p>
  
  <pre><code>Access to fetch at 'http://localhost:8080/id/azerty' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
  </code></pre>
  
  <p>What can I do to solve this problem ?
  Thanks</p>