如何为Firebase构建云功能以从多个文件部署多个功能? [英] How do I structure Cloud Functions for Firebase to deploy multiple functions from multiple files?

查看:138
本文介绍了如何为Firebase构建云功能以从多个文件部署多个功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Firebase创建多个云功能,并从一个项目同时部署它们。我还想将每个函数分成一个单独的文件。目前我可以创建多个函数,如果我将它们都放在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的云端功能正常加载节点模块,所以这个工作

Ah, Cloud Functions for Firebase load node modules normally, so this works

结构:

/functions
|--index.js
|--foo.js
|--bar.js
|--package.json

index.js:

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) => {
    ...
};

这篇关于如何为Firebase构建云功能以从多个文件部署多个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆