【问题标题】:How do I enable CORS for custom route in Sails.js如何在 Sails.js 中为自定义路由启用 CORS
【发布时间】:2017-05-30 23:28:46
【问题描述】:

我有一个 Angular 1.x 应用程序,它在我的 Sails.js 应用程序中调用 API。每当我尝试从我的 Angular 应用程序调用 API 时,我都会得到这个 -

XMLHttpRequest cannot load @987654321@. Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains the invalid value ''. Origin '@987654322@' is therefore not allowed access.

由于我的 Sails.js 应用程序有很多其他 API 不会在这个 Angular 应用程序上使用,我不想通过在 config/cors.js 中设置 allRoutes: true 来对所有这些 API 应用 CORS。所以我按照 Sails.js 的文档并以这种方式编写了自定义 CORS 配置 -

    '/portal/login': {
        target: 'MyController.login',
        cors: {
            origin: '*',
            credentials: true,
            methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH',
            headers: 'content-type, Authorization'
        }
    }

但它不起作用。如果我启用 allRoutes: true 然后它开始工作,但我不想在我的所有路由上启用 CORS 并公开它们。我已经尝试了origin, credentials, methods, headers 的所有可能组合,但它总是给出相同的错误。

你能帮我解决这个问题吗? 提前致谢。

【问题讨论】:

  • 如果您查看浏览器中的开发人员工具,您能看到 CORS 数据是如何发送的吗?使用它来验证您在请求和响应中获得了正确的质询/响应条目。有一些good resources 可以帮助您更好地理解CORS。值得努力阅读,因为理解 CORS 会对您有很大帮助,一旦您掌握了它的要点,这并不难。
  • 您是否尝试查看文档?似乎涵盖了这一点:sailsjs.com/documentation/concepts/security/…

标签: javascript angularjs node.js cors sails.js


【解决方案1】:

/config/http.js 文件中,取消注释 'order' 数组片段并在 myRequestLogger 方法中添加标头参数。

order: [
  'startRequestTimer',
  'cookieParser',
  'session',
  'myRequestLogger',
  'bodyParser',
  'handleBodyParserError',
  'compress',
  'methodOverride',
  'poweredBy',
  '$custom',
  'router',
  'www',
  'favicon',
  '404',
  '500'
],


 myRequestLogger: function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS, HEAD');
  res.header('Allow', 'GET, POST, PUT, DELETE, OPTIONS, HEAD');
  res.header('X-Powered-By', '');
  return next();
},

/config/cors.js (sails v0.12) 或 /config/security.js (sails v1.x) 内添加以下代码 sn -p

module.exports.security = { //sails v1.x, if is sails v0.12 change security for cors
  allRoutes: true,
  allowOrigins: '*',
  allowCredentials: false,
  allowRequestMethods: 'GET,POST,PUT,DELETE,OPTIONS,HEAD',
  allowRequestHeaders: 'content-type'
}

如果这些都不起作用(我发现这很难发生),请添加您的 controller 方法的返回(但是,我认为没有必要):

return res.set({
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin': '*',
  }).json({ status: true, //body });

【讨论】:

    【解决方案2】:

    这在 \config\security.js 中对我有用

    cors: {
    allRoutes: true,
        allowOrigins: [
      'http://localhost:1337',
      'http://localhost:8100',
      'http://localhost',
        ],
        allowCredentials: true,
        allowRequestHeaders:'content-type'
    //allowCredentials: false
    

    },

    【讨论】:

    • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的答案添加解释并说明适用的限制和假设。
    【解决方案3】:

    您可以通过在配置文件夹内的Sails.js 安全文件中启用 CORS 来实现:

    config/security.js
    

    这是它的代码:

    cors: {
        allRoutes: true,
        allowOrigins: '*',
        allowCredentials: false,
    }
    

    您可以在Sails.js 官方文档中查看它的参考: https://sailsjs.com/documentation/concepts/security/cors

    【讨论】:

    • 问题是:“如果我启用 allRoutes: true 那么它开始工作,但我不想在我的所有路由上启用 CORS 并公开它们”
    猜你喜欢
    • 2013-09-04
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多