安装命令
npm/cnpm install egg-cors --save yarn add egg-cors --save添加相关配置
在egg项目中 config/plugins.js 添加以下配置 module.exports = { cors: { enable: true, package: 'egg-cors' } } 在egg项目中 config/config.default.js 添加以下配置 module.exports = { const config = exports = {} config.security = { csrf: { enable: false, ignoreJSON: true }, domainWhiteList: [ '*' ], // 白名单 }; config.cors = { origin: '*', // 如果不写origin则会按照白名单中的域名允许跨域, * 代表允许所有的域名进行跨域请求 allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH' } }