使用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