【问题标题】:CORS block in firebase cloud functionFirebase 云功能中的 CORS 块
【发布时间】:2021-03-05 10:16:21
【问题描述】:

我正在实现一个云功能以从 BigQuery 获取结果。下面是我的函数/index.js 代码

const functions = require('firebase-functions');
const {BigQuery} = require('@google-cloud/bigquery');
const cors = require('cors')({origin: true});

exports.getBigQueryData = functions.region('europe-west3').https.onRequest((req,res) => {
  cors(req,res,() => {
    const bigquery = new BigQuery({
      projectId: 'neon-opus-585',
      keyFilename: 'service_account_bq.json'
    });
    const query = "SELECT * FROM `xxxx`";
    bigquery.createQueryJob({query: query}).then((data) => {
      const job = data[0];
      return job.getQueryResults();
    })
    .then(results => {return res.send(results)})
    .catch(error => {console.log(error)})
  })
})

当我只部署/测试该功能时,一切运行正常,但是当我从我的应用程序内部使用它时,我收到以下错误

CORS 策略已阻止从源“http://localhost:5000”访问“https://xxxx.cloudfunctions.net/xxxx”获取:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。

如您所见,我尝试导入 cors 包并在我的函数中实现它,正如这里的几篇帖子所建议的那样,但它仍然无法正常工作。

任何帮助将不胜感激:)

【问题讨论】:

    标签: node.js firebase google-cloud-functions cors


    【解决方案1】:

    看看Handling CORS requests,你有一个例子:

    要处理预检请求,您必须设置适当的 Access-Control-Allow-* 标头以匹配您要接受的请求:

    exports.corsEnabledFunction = (req, res) => {
      // Set CORS headers for preflight requests
      // Allows GETs from any origin with the Content-Type header
      // and caches preflight response for 3600s
    
      res.set('Access-Control-Allow-Origin', '*');
    
      if (req.method === 'OPTIONS') {
        // Send response to OPTIONS requests
        res.set('Access-Control-Allow-Methods', 'GET');
        res.set('Access-Control-Allow-Headers', 'Content-Type');
        res.set('Access-Control-Max-Age', '3600');
        res.status(204).send('');
      } else {
        res.send('Hello World!');
      }
    };
    

    还有Authentication with CORS

    如果您计划发送带有 Authorization 标头的请求,您必须:

    1. 将 Authorization 标头添加到 Access-Control-Allow-Headers。
    2. 将 Access-Control-Allow-Credentials 标头设置为 true。
    3. 在 Access-Control-Allow-Origin 中设置特定来源(不接受通配符)。
    exports.corsEnabledFunctionAuth = (req, res) => {
      // Set CORS headers for preflight requests
      // Allows GETs from origin https://somedomain.com with Authorization header
    
      res.set('Access-Control-Allow-Origin', 'https://somedomain.com');
      res.set('Access-Control-Allow-Credentials', 'true');
    
      if (req.method === 'OPTIONS') {
        // Send response to OPTIONS requests
        res.set('Access-Control-Allow-Methods', 'GET');
        res.set('Access-Control-Allow-Headers', 'Authorization');
        res.set('Access-Control-Max-Age', '3600');
        res.status(204).send('');
      } else {
        res.send('Hello World!');
      }
    };
    

    【讨论】:

    猜你喜欢
    • 2018-08-27
    • 2018-05-15
    • 2021-04-08
    • 2020-01-22
    • 2021-03-21
    • 2021-06-07
    • 2020-10-30
    • 2020-06-09
    • 2020-04-27
    相关资源
    最近更新 更多