前言
Egg 项目中关闭csrf,使用 egg-cors处理跨域问题
文档:https://www.npmjs.com/package/egg-cors
一、安装
npm i egg-cors --save
二、配置跨域插件 config/plugin.js
//跨域问题
exports.cors = {
enable: true,
package: 'egg-cors'
}
三 、config / config.default.js 目录下配置
config.security = {
// 关闭 csrf
csrf: {
enable: false,
},
// 跨域白名单
domainWhiteList: [ 'http://localhost:3000' ],
};
// 允许跨域的方法
config.cors = {
origin: '*',
allowMethods: 'GET, PUT, POST, DELETE, PATCH'
};