[英]Can't we Write multiple files in Firebase Cloud Functions
我們如何使用 Firebase Cloud Functions 實現微服務架構 我們可以編寫多個 .js 文件,而不是將所有函數都寫入 index.js,這樣我們就不需要重新部署所有函數來更改單個函數
我正在導入帶有 firebase 函數的 other.js 文件。 只需將 functions 文件夾視為根目錄,我錯誤地嘗試從 /functions 父文件夾導入文件。
索引.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
您的所有 Cloud Functions for Firebase 都必須在index.js
文件中定義。 但這並不意味着您必須在單個文件中實現所有功能。
我經常在單獨的文件中實現每個函數的大部分。 例如,如果我使用 Google Cloud Vision API 從圖像中提取額外的文本,我將有一個ocr.js
。 我給這個文件一個主要部分,這樣我就可以使用node ocr.js
從本地終端運行腳本。 然后在我的index.js
中,除了導入ocr.js
並將其連接到 Cloud Functions 之外,我只需要更多的代碼。
另見:
您好,您可以通過以下方式進行。
阿爾法.js
const functions = require('firebase-functions');
exports.alphaFunction = functions.https.onRequest((request, response) => {
// Your code
});
索引.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();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.