npm i egg-cors --save // 或 yarn add egg-cors
// 在config/plugin.js里面添加
exports.cors = {
  enable: true,
  package: ‘egg-cors‘,
};
// 或者,在config/plugin.js的module.exports里面添加
module.exports = {
  cors: {
    enable: true,
    package: ‘egg-cors‘,
  }
};
// 在config/config.default.js的module.exports里面添加
config.cors = {
  origin: ‘*‘, // 表示允许的源
  allowMethods: ‘GET,HEAD,PUT,POST,DELETE,PATCH‘ // 表示允许的http请求方式
};
.
原文:https://www.cnblogs.com/crazycode2/p/12445924.html