關(guān)于使用koa 跨域問題你可能會遇到

var cors = require('koa2-cors');
// 跨域
const allowOrigins = [
    "http://localhost:8080" // 需要跨域的端口,與本服務(wù)器端口不一樣,請注意。
];
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'],
}));

作者:Vam的金豆之路

主要領(lǐng)域:前端開發(fā)

我的微信:maomin9761

微信公眾號:前端歷劫之路