【问题标题】:Can't we Write multiple files in Firebase Cloud Functions我们不能在 Firebase Cloud Functions 中写入多个文件吗
【发布时间】:2017-12-23 04:23:21
【问题描述】:

我们如何使用 Firebase Cloud Functions 实现微服务架构 我们可以写多个 .js 文件,而不是将所有函数都写入 index.js,这样我们就不需要重新部署所有函数来更改单个函数

【问题讨论】:

    标签: firebase google-cloud-functions


    【解决方案1】:

    您好,您可以通过以下方式执行此操作。

    alpha.js

    const functions = require('firebase-functions');
    exports.alphaFunction = functions.https.onRequest((request, response) => {
       // Your code
    });
    

    index.js

    const functions = require('firebase-functions');
    var alphaFunction = require('./alpha');
    exports.mainFunction = functions.https.onRequest((request, response) => {
        //Inside your main function
        exports.alphaFunction = alphaFunction.alphaFunction();
    });
    

    【讨论】:

      【解决方案2】:

      我正在导入其他具有 firebase 功能的 .js 文件。把functions文件夹想象成root,我错误地试图从/functions父文件夹导入文件。

      index.js

      var paymentFunctions = require('./payment_functions');
      

      还有类似的东西:

      exports.paymentMethodTask = functions.database.ref('/newPaymentMethodTask/{taskId}').onWrite(event => {
          return paymentFunctions.processPaymentMethodTask(event);
      });
      

      文件夹结构:

      /myProject/functions/index.js
      /myProject/functions/payment_functions.js
      

      然后像往常一样在 payment_functions.js 中导出你的函数:

      module.exports = {
          processPaymentMethodTask: function test(event) {
              //do something here with the event
          }
      };
      

      https://medium.com/step-up-labs/our-experience-with-cloud-functions-for-firebase-d206448a8850

      【讨论】:

        【解决方案3】:

        您的所有 Cloud Functions for Firebase 都必须在 index.js 文件中定义。但这并不意味着您必须在一个文件中实现所有功能。

        我经常在一个单独的文件中实现每个函数的大部分。例如,如果我使用 Google Cloud Vision API 从图像中提取文本,我将拥有一个ocr.js。我给这个文件一个主要部分,以便我可以使用node ocr.js 从本地终端运行脚本。然后在我的 index.js 中,我只需要导入 ocr.js 并将其连接到 Cloud Functions 即可。

        另见:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-26
          • 1970-01-01
          • 1970-01-01
          • 2018-10-08
          • 2019-01-04
          • 1970-01-01
          • 2021-05-20
          • 2019-08-01
          相关资源
          最近更新 更多