~~~
const koa = require('koa');
const app = new koa();
var cors = require('koa2-cors');
app.use(cors({
origin: function (ctx) {
//访问主域名下的test地址不允许跨域
if (ctx.url === '/test') {
return false;
}
return '*';
},
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
maxAge: 5,
credentials: true,
allowMethods: ['GET', 'POST', 'DELETE'],
allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}));
app.use(async ctx=>{
ctx.body = {name:"chengchao"}
})
app.listen(8080)
~~~