1.安装中间件
npm install egg-cors --save
2.在 config/plugin.js
文件中启用 egg-cors
插件:
/** @type Egg.EggPlugin */
module.exports = {
cors: {
enable: true,
package: "egg-cors",
},
};
3.在 config/config.default.js
文件中添加跨域插件配置:
module.exports = (appInfo) => {
const config = (exports = {});
config.cors = {
origin: "*", // 允许所有的请求源
allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH",
};
config.security = {
csrf: {
enable: false,
},
};
return {
...config,
};
};