使用EggJS开发接口(一)允许跨域之egg-cors


用来允许前端进行跨域请求的一个插件
安装:
npm i egg-cors --save
// 或
yarn add egg-cors
导入到egg项目里:
// 在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请求方式
};

.


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM