egg接口跨域配置

    技术2022-07-29  79

    安装egg-cors

    安装命令 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' } }
    Processed: 0.009, SQL: 9