美文网首页前端杂记
解决Egg跨域问题

解决Egg跨域问题

作者: 永远的八神 | 来源:发表于2019-02-20 17:42 被阅读0次

    主要是用到了egg-cors这个包,再进行配置一下就ok了

    步骤一:
    # 下载 egg-cors 包
    npm i egg-cors --save
    
    步骤二:
    # 在plugin.js中设置开启cors 
    exports.cors = {
      enable: true,
      package: 'egg-cors',
    };
    
    步骤三:
    # 在config.{env}.js中配置,注意配置覆盖的问题
    config.security = {
      csrf: {
        enable: false,
        ignoreJSON: true
      },
      domainWhiteList: '*'
    };
    
    config.cors = {
      origin:'*',
      allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
    };
    

    或者下面的,步骤都一样,放出的白名单不一样而已

    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'
    };
    

    相关文章

      网友评论

        本文标题:解决Egg跨域问题

        本文链接:https://www.haomeiwen.com/subject/pgzhyqtx.html