繁体   English   中英

Firebase:如何在HTTP功能下动态包含模块

[英]Firebase: How to dynamically include modules under an HTTP function

我正在尝试在Firebase上构建一些功能的开源库,人们可以克隆该库并将其部署到自己的Firebase安装中。 我们的功能允许用户选择带有JSON配置文件的模块集合,然后对于所选的每个模块,我们的functions/index.js应该将该功能作为HTTP函数加载。 该文件将如下所示。

{modules: [{
    name: 'ModuleOne',
    options: {
        something: true
    }
}, {
    name: 'ModuleThree',
    options: {
        something: false
    }
}, {
    name: 'ModuleFour',
    options: {
        something: true
    }
}]}

Firebase云功能的HTTP URL如下所示:

https://us-central1-projectname-dev.cloudfunctions.net/OurProjectName/ModuleOne https://us-central1-projectname-dev.cloudfunctions.net/OurProjectName/ModuleThree https:// us-central1-projectname-dev。 cloudfunctions.net/OurProjectName/ModuleFour

我们如何创建一个Firebase云功能,使我们能够动态选择这样的HTTP模块并基于此配置启用它们? 我们可以将动态模块路径嵌套在OurProjectName下,所以我们可以使用一个路由器来在其中添加动态加载路径以加载模块的功能吗?

认为您在这里寻找的实际上是创建HTTP函数工厂。 这实际上不是很困难。 您可以执行以下操作:

const functions = require('firebase-functions');
const express = require('express');

module.exports = function(options) {
  const app = express();
  options.modules.forEach(moduleConfig => {
    // add express routes for each module
  });
  return functions.https.onRequest(app);
};

然后,开发人员将像这样使用您的库:

const yourlib = require('yourlib');

exports.someFnName = yourlib({
  modules: [
    {name: 'ModuleOne', options: {}}
  ]
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM