加载中...
  • egg.js 配置cors跨域

    1、egg简述

    Egg.js,为企业级框架和应用而生,是阿里开源的企业级 Node.js 框架。

    2、特点

    Egg 奉行『约定优于配置』,按照一套统一的约定进行应用开发,团队内部采用这种方式可以减少开发人员的学习成本。

    基于 Koa 开发,性能优异。

    3、基于eggjs的 restful api示例

    https://github.com/MengFangui/eggjs-api

    4、跨域的解决

    1)安装依赖包

    1
    2
    3
    npm i egg-cors --save
    or
    yarn add egg-cors

    2)在plugin.js中设置开启cors

    1
    2
    3
    4
    exports.cors = {
    enable: true,
    package: 'egg-cors',
    };

    3)在config.default.js中配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    config.security = {
    csrf: {
    enable: false,
    ignoreJSON: true
    },
    domainWhiteList: ['http://localhost:8080']
    };
    config.cors = {
    origin:'*',
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
    };

    说明需要配置security ,不然会报403权限错误。  

    上一篇:
    egg.js中extend框架扩展和调用
    下一篇:
    egg.js之解决跨域问题(egg-cors)
    本文目录
    本文目录