首页 > 解决方案 > Firebase 函数 - 多个 js 文件来组织函数

问题描述

我有一个名为 的文件夹functions,里面有index.js. 在index.js我有接受所有 http 的主要功能:

 exports.contentServer = functions.https.onRequest((request, response) => {

由于我有更多的功能,我需要组织我的文件,并且我想添加另一个文件,比如说payments.js,以便index.js 可以调用里面的函数 payments.js,或者甚至onCreate会从payment.js.

如果我只是在 中创建另一个js文件functions,它将无法正常工作。

必须做什么才能在另一个 js文件中拥有一个云功能?

标签: javascriptnode.jsfirebasegoogle-cloud-functions

解决方案


您可以使用标准 nodejsrequire在其他文件中加载代码。

index.js

const otherFunctions = require('./other.js');

exports.contentServer = functions.https.onRequest((request, response) => {
     otherFunctions.other()
}

其他.js

exports.other = function() { ... }

我建议阅读文档以获取更多信息。


推荐阅读