简体   繁体   English

用于基于CORS预检请求的Firebase触发功能的云功能

[英]Cloud Functions for Firebase triggering function on CORS preflight request

I have a cloud function that validates inputs from a form submission on my client. 我有一个云功能,可以验证客户端表单提交的输入。 I'm using Cloud Functions for Firebase https triggers with cors express middleware . 我正在使用Cloud Functions for Firebase https触发器cors表达中间件

Firebase Function Firebase功能

const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors')({origin: true});
const validateImageForm = require('./library/validate-image-form');

exports.apiValidateImageForm = functions.https.onRequest((req, res) => {

  cors(req, res, () => {

    validateImageForm(req.body.formInputs, req.body.imageStatus).then((data) => {
      res.status(200).send(data);
    });

  });

});

Client Call To Function 客户呼叫功能

const validateImageFormFetchOptions = {
   headers: {
     'Accept': 'application/json',
     'Content-Type': 'application/json'
   },
   method: 'POST',
   body: JSON.stringify({
     formInputs: formInputs
   })
}

fetch('https://project-url.cloudfunctions.net/apiValidateImageForm', validateImageFormFetchOptions)
 .then(response => response.json())
 .then(serverErrors => {console.log(serverErrors)});

Issue 问题

When I call this function from my client using a fetch request I see two apiValidateImageForm triggers in the functions logs. 当我使用获取请求从我的客户端调用此函数时,我在函数日志中看到两个apiValidateImageForm触发器。 The first is a status 204 and I assume this comes from the cors preflight request that checks the origin. 第一个是状态204,我认为这来自检查原点的cors预检请求。 The final request is status 200. I just want one function trigger when I fetch the apiValidateImageForm function. 最终请求是状态200.当我获取apiValidateImageForm函数时,我只想要一个函数触发器。 I'm concerned that over time the preflight requests that output the status 204 will add unnecessary function calls to my project quota. 我担心随着时间的推移,输出状态204的预检请求会为我的项目配额添加不必要的函数调用。

在此输入图像描述

Question

Is it possible to prevent firebase from triggering a function call on the preflight request? 是否可以阻止firebase触发预检请求的函数调用? If not then is there a way to prevent the preflight request and successfully pass data to the function. 如果没有,那么有没有办法阻止预检请求并成功将数据传递给函数。

To fix the duplicate requests 200 and 204 then change how you're client side fetch request. 要修复重复请求200和204,请更改您的客户端获取请求的方式。 @sideshowbarker is right. @sideshowbarker是对的。 The browser automatically does the CORS preflight OPTIONS request so this is not an issue in Cloud Functions for Firebase. 浏览器会自动执行CORS预检OPTIONS请求,因此这不是Cloud Functions for Firebase中的问题。 This answer was helpful. 这个答案很有帮助。

To fix the preflight I changed my code to the following: 为了修复预检,我将代码更改为以下内容:

Client Call To Function 客户呼叫功能

Removed the headers from the fetch options completely rather than setting the content type as application/json. 完全从提取选项中删除了标题,而不是将内容类型设置为application / json。 By default the fetch request content type is application/x-www-form-urlencoded; 默认情况下,获取请求内容类型为application / x-www-form-urlencoded; charset=UTF-8. 字符集= UTF-8。

const validateImageFormFetchOptions = {
  method: 'POST',
  body: JSON.stringify({
   formInputs: formInputs
  })
}

fetch('https://project-url.cloudfunctions.net/apiValidateImageForm', validateImageFormFetchOptions)
  .then(response => response.json())
  .then(serverErrors => {console.log(serverErrors)});

Firebase Function Firebase功能

Explicitly parsed the request body because it is now received as a text string. 显式解析请求正文,因为它现在作为文本字符串接收。

const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors')({origin: true});
const validateImageForm = require('./library/validate-image-form');

exports.apiValidateImageForm = functions.https.onRequest((req, res) => {
  cors(req, res, () => {
    const body = JSON.parse(req.body);
    validateImageForm(body.formInputs, body.imageStatus).then((data) => {
      res.status(200).send(data);
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM