关于使用koa 跨域问题你可能会遇到

1,370 阅读1分钟
var cors = require('koa2-cors');
// 跨域
const allowOrigins = [
    "http://localhost:8080" // 需要跨域的端口,与本服务器端口不一样,请注意。
];
app.use(cors({
    origin: function(ctx) {
      if (allowOrigins.includes(ctx.header.origin)) {
        return ctx.header.origin;
      }
      return false;
    },
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
    maxAge: 5,
    credentials: true,
    withCredentials:true,
    allowMethods: ['GET', 'POST', 'DELETE'],
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}));