如何构建 Cloud Functions for Firebase 以从多个文件部署多个函数? [英] How do I structure Cloud Functions for Firebase to deploy multiple functions from multiple files?
问题描述
我想为 Firebase 创建多个 Cloud Functions 并从一个项目同时部署它们.我还想将每个函数分成一个单独的文件.目前,如果我将它们都放在 index.js 中,我可以创建多个函数,例如:
I would like to create multiple Cloud Functions for Firebase and deploy them all at the same time from one project. I would also like to separate each function into a separate file. Currently I can create multiple functions if I put them both in index.js such as:
exports.foo = functions.database.ref('/foo').onWrite(event => {
...
});
exports.bar = functions.database.ref('/bar').onWrite(event => {
...
});
但是我想将 foo 和 bar 放在单独的文件中.我试过这个:
However I would like to put foo and bar in separate files. I tried this:
/functions
|--index.js (blank)
|--foo.js
|--bar.js
|--package.json
foo.js 在哪里
exports.foo = functions.database.ref('/foo').onWrite(event => {
...
});
和 bar.js 是
exports.bar = functions.database.ref('/bar').onWrite(event => {
...
});
有没有办法在不将所有函数都放在 index.js 中的情况下实现这一点?
Is there a way to accomplish this without putting all functions in index.js?
推荐答案
啊,Firebase 加载节点模块的 Cloud Functions 函数正常,所以这有效
Ah, Cloud Functions for Firebase load node modules normally, so this works
结构:
/functions
|--index.js
|--foo.js
|--bar.js
|--package.json
index.js:
const functions = require('firebase-functions');
const fooModule = require('./foo');
const barModule = require('./bar');
exports.foo = functions.database.ref('/foo').onWrite(fooModule.handler);
exports.bar = functions.database.ref('/bar').onWrite(barModule.handler);
foo.js:
exports.handler = (event) => {
...
};
bar.js:
exports.handler = (event) => {
...
};
这篇关于如何构建 Cloud Functions for Firebase 以从多个文件部署多个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!