node egg.js 跨域设置

1.下载依赖

npm install  egg-cors --save

如果失败,可以配置淘宝镜像下载。

npm install  egg-cors --save --registry=http://registry.npm.taobao.org

2.app/config/plugin.js文件添加

// 跨域设置
exports.cors = {
  enable: true,
  package: "egg-cors"
};

整个文件:

'use strict';

// 跨域设置
exports.cors = {
  enable: true,
  package: "egg-cors"
};

3.app/config/config…default.js 文件添加

 // 跨域设置
  config.cors = {
    origin: "*",
    allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH"
  };

整个文件:

/* eslint valid-jsdoc: "off" */

'use strict';

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1581684233832_516';

  // 跨域设置
  config.cors = {
    origin: "*",
    allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH"
  };

  // add your middleware config here
  config.middleware = [];

  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };

  return {
    ...config,
    ...userConfig,
  };
};

你可能感兴趣的:(#,node)