【问题标题】:How can I using hosting rewirtes to connect between firebase hosting and cloud functions如何使用托管重写来连接 Firebase 托管和云功能
【发布时间】:2020-12-27 19:51:12
【问题描述】:

用于更具体描述的新内容

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

但是当我尝试获取 Firebase 云功能时出现此错误

TypeError:无法读取 /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) 在 Function.handle (/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) 在 /usr/local/lib/node_modules/ firebase-tools/lib/emulator/functionsEmulatorRuntime.js:593:20 at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:568:19 at Generator.next () at /usr/ local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:8:71 at new Promise () at __awaiter (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js: 4:12)

/functions/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)

/functions/api.js

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

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

module.exports = router

谁能给我一个线索来解决这个问题?


以前的内容

我正在尝试将我的 node(with express) 项目转换为与 Firebase 兼容的项目。

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

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/apilocalhost:5000/api/sentences 时,我可以得到回应,但它不起作用。

为此,我尝试.onCall 提交我的云功能,而不是使用.onRequest

但是当我调用functions.httpsCallable("endpoint")时只接受了POST请求。

所以,我尝试使用 onRequest 和 rewrites 在 firebase.json 中托管

"hosting": {
    "public": "public",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "/api/**",
        "function": "api"
      }
    ]
  },

但是,我刚刚收到这个错误:

TypeError:无法读取 /Users/fredriccliver/Projects/Speech/functions/node_modules/express/lib/router/index.js:635:15 处未定义的属性“应用”

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

【问题讨论】:

  • 在转换应用时,是否同时将 Cloud Function 移至 Firebase Cloud Function
  • @ShawnDiWu 是的,我将从我的 express nodejs 项目中使用 firebase 的所有功能。您的意思是我打算同时使用托管和功能吗?我基于firebase控制台。所以我的意思是我会将我的快速路由转换为 Firebase 云功能

标签: firebase express google-cloud-functions firebase-hosting


【解决方案1】:

您的路由应包含从 Firebase 托管代理的 URI 的完整路径。

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

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

我相信您也可以使用use() 为应用中的每条路由指定一个公共前缀。

【讨论】:

  • 非常感谢,道格。就在发布我的问题之后,我再次尝试了你的 youtube 剪辑。我将再次尝试使用 use() 方法进行路由。提前,我也对以编程方式或自动方式检索函数主要端点的完整路径感到好奇。 stackoverflow.com/questions/63815450/…
  • 我更具体地修改了我的问题。我在尝试应用路由时遇到了错误。
【解决方案2】:

我在检查了一个 youtube 剪辑并尝试了更多之后解决了。

在我的中型帖子中进行了总结。 https://medium.com/@fredriccliver/how-to-use-an-express-router-within-the-cloud-function-developing-environment-cb64face4043

functions/index.js

const functions = require("firebase-functions")
const express = require("express")
const app = express()

// my routings
const apiRoute = require("./api")

// add routes to the express app.
app.use("/api", apiRoute)

exports.api = functions.https.onRequest(app)

functions/api.js

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

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

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

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

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

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

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

module.exports = router

/firebase.json


{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [{
      "source": "/api/**",
      "function": "api"
    }]
  }
}

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

【讨论】:

    猜你喜欢
    • 2020-06-13
    • 2018-04-11
    • 2017-12-11
    • 1970-01-01
    • 2020-05-14
    • 2020-03-15
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多