How can we implement Micro-service architecture using Firebase Cloud Functions can we write multiple .js files unlike writing all functions into index.js so that we need not re-deploy all functions for change in single function
I'm importing other .js files with firebase functions. Just think of the functions folder as root, I was mistakenly trying to import files from /functions parent folder.
index.js
var paymentFunctions = require('./payment_functions');
along with something like:
exports.paymentMethodTask = functions.database.ref('/newPaymentMethodTask/{taskId}').onWrite(event => {
return paymentFunctions.processPaymentMethodTask(event);
});
With the folder structure:
/myProject/functions/index.js
/myProject/functions/payment_functions.js
Then export your functions in payment_functions.js as normal:
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
All your Cloud Functions for Firebase will have to be defined in the index.js
file. But that doesn't mean you have to implement all functionality in a single file.
I often implement the bulk of each function in a separate file. For example, if I'm using the Google Cloud Vision API to extra text from images, I'll have an ocr.js
. I give this file a main section, so that I can run the script from a local terminal using node ocr.js
. Then in my index.js
I need little more code than an import of ocr.js
and wiring that up to Cloud Functions.
Also see:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With