我如何使用主机重接器连接firebase主机和云功能

我如何使用主机重接器连接firebase主机和云功能,firebase,express,google-cloud-functions,firebase-hosting,Firebase,Express,Google Cloud Functions,Firebase Hosting,用于更具体描述的新内容 我正在尝试使用express和firebase功能进行高级路由 但我在尝试获取firebase云函数时遇到了这个错误 TypeError:无法在Function.handle处读取未定义的/Users/fredriccliver/Projects/firebase test/functions/node_modules/express/lib/router/index.js:635:15(/Users/fredriccliver/Projects/firebase tes

用于更具体描述的新内容

我正在尝试使用express和firebase功能进行高级路由

但我在尝试获取firebase云函数时遇到了这个错误

TypeError:无法在Function.handle处读取未定义的/Users/fredriccliver/Projects/firebase test/functions/node_modules/express/lib/router/index.js:635:15(/Users/fredriccliver/Projects/firebase test/functions/node_modules/express/lib/router/index.js:260:14)处的属性“apply”路由器上的(/Users/fredriccliver/Projects/firebase test/functions/node_modules/express/lib/router/index.js:174:3)(/Users/fredriccliver/Projects/firebase test/functions/node_modules/express/lib/router/index.js:47:12)at/usr/local/lib/node_modules/firebase tools/lib/emulator/functionsemulatoruntime.js:593:20 at/usr/local/lib/node_modules/firebase tools/lib/emulator/functionsemulatoruntime.js:568:19 at Generator.next()at/usr/local/lib/lib/emulator/functionsemulatoruntime.js:8:71 at new Promise()at_uwaiter(/usr/local/lib/node_modules/firebase tools/lib/emulator/functionsemulatoruntime.js:4:12)

/函数/index.js

const functions = require("firebase-functions")
const admin = require("firebase-admin")
const cors = require("cors")
const express = require("express")
const apiRoute = require("./api")

// admin.initializeApp(functions.config().firebase)

const app = express()
app.use(cors)

app.use("/api", apiRoute)

exports.api = functions.https.onRequest(apiRoute)
const apiRoute = require("./routes/api")
exports.api = functions.https.onRequest(apiRoute)
/函数/api.js

const router = require("express").Router()

router.get("/data", (req, res) => {
  res.send(`this is data`)
})

module.exports = router
谁能给我一个解决这个问题的线索吗


以前的内容

我正在尝试将我的节点(带有express)项目转换为与Firebase兼容的项目

我将我的API端点添加到 /函数/index.js

const functions = require("firebase-functions")
const admin = require("firebase-admin")
const cors = require("cors")
const express = require("express")
const apiRoute = require("./api")

// admin.initializeApp(functions.config().firebase)

const app = express()
app.use(cors)

app.use("/api", apiRoute)

exports.api = functions.https.onRequest(apiRoute)
const apiRoute = require("./routes/api")
exports.api = functions.https.onRequest(apiRoute)
在我的api.js中

router.get("/", (req, res) => {
  res.send("api is running on")
})

router.get("/sentences", (req, res) => {
  res.send("hi")
})
所以,我的期望是,当我调用
localhost:5000/api
localhost:5000/api/句子时,我可以得到一个响应,但它不起作用

为此,我尝试了
.onCall
来提交我的云函数,而不是使用
.onRequest

但当我调用
functions.httpscalable(“端点”)
时,只接受了POST请求

因此,我尝试在firebase.json中使用onRequest和rewrites

"hosting": {
    "public": "public",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "/api/**",
        "function": "api"
      }
    ]
  },
但是,我刚刚得到一个错误:

TypeError:无法在下一步读取未定义的at/Users/fredriccliver/Projects/Speech/functions/node_modules/express/lib/router/index.js:635:15的属性“apply”


我应该如何从前端javascript调用我的函数?

您的路由应该包括从Firebase主机代理的URI的完整路径

router.get("/api", (req, res) => {
  res.send("api is running on")
})

router.get("/api/sentences", (req, res) => {
  res.send("hi")
})

我相信您也可以使用为应用程序中的每个路由指定一个通用前缀。

您的路由应该包括从Firebase主机代理的URI的完整路径

router.get("/api", (req, res) => {
  res.send("api is running on")
})

router.get("/api/sentences", (req, res) => {
  res.send("hi")
})

我相信你也可以使用为应用程序中的每条路线指定一个通用前缀。

我在查看youtube剪辑并尝试了更多内容后解决了这个问题

在我的中篇文章中总结。

函数/index.js

函数/api.js

/firebase.json


因此,在查看youtube视频剪辑并尝试更多内容后,我可以接近所有端点

在我的中篇文章中总结。

函数/index.js

函数/api.js

/firebase.json


所以,我可以接近所有端点

当你转换你的应用程序时,你是否同时将云功能移动到了?@ShawnDiWu是的,我将使用我的express nodejs项目中firebase的所有功能。你是说我打算同时使用托管和功能吗?我基于firebase控制台。所以我的意思是我将我的快速路由到firebase云功能转换应用程序时,是否同时将云功能移动到?@ShawnDiWu是的,我将使用我的快速nodejs项目中firebase的所有功能。你是说我打算同时使用托管和功能吗?我基于firebase控制台。所以我的意思是I’我会将我的快速路由转换为firebase云功能非常感谢你,道格。在发布我的问题后,我又尝试了你的youtube剪辑,所以我会尝试使用()再次路由的方法。首先,我还想通过编程或自动检索函数主端点的完整路径。我更具体地修改了我的问题。我在尝试应用路由时遇到了错误。非常感谢你,Doug。在发布我的问题后,我再次尝试了你的youtube剪辑。因此,我不会请再次使用use()方法进行路由。首先,我还想通过编程或自动检索函数主端点的完整路径。我更具体地修改了我的问题。我在尝试应用路由时遇到了错误。