Egg.js中配置跨域与路由分组

287 阅读1分钟

一、跨域配置

egg.js中实现跨域主要是通过egg-cors这个插件,更多信息可以通过npm官网去查看这个插件的用法。

  1. 安装插件
cnpm i egg-cors --save
  1. 在plugin.js中配置
  cors: {
    enable: true,
    package: 'egg-cors'
  }
  1. 在config.default.js中配置
config.cors = {
    origin: '*',
    allowMethods: 'GET,PUT,POST,DELETE'
  }

  config.security = {
    csrf: {
      ignore: ctx => {
        if (ctx.request.url === `/${config.adminPath}/product/doUpload`) {
          return true;
        } else {
          return false;
        }
      }
    },
    domainWhiteList: ['http://localhost:8081']
  }

二、设置前端API路由POST数据无需进行CSRF验证

只需在config.default.js中的csrf属性配置中进行配置即可。

  config.security = {
    csrf: {
      ignore: ctx => {
        if (ctx.request.url === `/${config.adminPath}/product/doUpload`) {
          return true;
        } else if (ctx.request.url.indexOf("/api") != -1) {
          return true
        } else {
          return false;
        }
      }
    },
    domainWhiteList: ['http://localhost:8081']
  }

三、获取数据库中指定字段的数据

主要是通过attributes这个字段来进行获取。

let result = await this.ctx.model.ProductCate.findAll({
  include: {
    model: this.ctx.model.Product,
    attributes: ['id','cid','title','price','imgUrl','sort']
  }
});

如果想要将获取到的数据按照某种顺序进行排列,可以通过order属性进行配置。

let result = await this.ctx.model.ProductCate.findAll({
  include: {
    model: this.ctx.model.Product,
    attributes: ['id','cid','title','price','imgUrl','sort']
  },
  order: [
    ['sort','DESC'],
    [this.ctx.model.Product,'sort','DESC']
  ]
});