首页 > 解决方案 > firebase node.js/express.js 添加多个firebase函数

问题描述

我正在尝试index.ts按照此处的示例在文件中添加多个 Firebase 函数:如何构建 Cloud Functions for Firebase 以从多个文件部署多个函数?.

特别是,我有一个express.js初始化的服务器routes.ts

const server = express();
server.get('/api', (req, res) => res.send('hello /api'))
export {server}

现在在 index.ts 中,我可以导入routes.ts并附加一个用户身份验证侦听器,以便在创建新用户时触发,即:

import {server} from './routers'

exports.app = functions.https.onRequest(server);
exports.userEvents = functions.auth.user().onCreate(user => {
    console.log("user created: ", user.email, user)
})

然而,据我所知exports.app ,它实际上是按预期工作的,并且exports.userEvents似乎不会在创建新用户时触发。

==================================================== =======

编辑:

问题是我的firebase.json函数是如何设置的,它目前只服务于app以 开头的端点/api/**,即:

{
  "hosting": {
    "public": "public",

    // the problem is here: you're not connecting to multiple functions
    "rewrites": [{
        "source": "/api/**"
      , "function": "app"  



      }],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ]
  }
}

现在的问题是我如何告诉 firebase 从exports.userEvents...提供功能

标签: node.jsfirebaseexpressgoogle-cloud-functionsfirebase-cli

解决方案


问题是我试图模拟本地环境中的功能。我必须部署它们http才能运行非功能。但是,文档底部描述了其他功能的本地模拟器:https ://firebase.google.com/docs/functions/beta-v1-diff ,这是新的。


推荐阅读