Search code examples
node.jskoa

After adding cors to nodeJS returning internal server error


I'm stuck with cors issue.I have to get data on another port, so i need cors dependency. there appears problem. everything works fine without app.use(cors()); but when i add it, server returns internal server error Error code 500

const Koa = require("koa");
const Router = require("koa-router");

const app = new Koa();
const router = Router();

const data = require("./data.json");
const cors = require("cors");

app.use(cors({ origin: true }));

router.get("/", async (ctx, next) => {
  ctx.body = data;
});

app.use(router.routes());

app.listen(3001);


Solution

  • I added koajs/cors instead of cors and it solved problem