egg.js之解决跨域问题(egg-cors)

1.下载 egg-cors 包
npm i egg-cors --save
2.在plugin.js中设置开启cors
exports.cors = {
  enable: true,
  package: 'egg-cors',
};
3.在config.default.js中配置
// add your config here
config.middleware = [];
//多出来的配置==========
config.security = {
  csrf: {
    enable: false,
    ignoreJSON: true
  },
  domainWhiteList: ['http://localhost:8080']
};
config.cors = {
  origin:'*',
  allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
};

推荐阅读更多精彩内容