如何使用托管轮播在Firebase托管和云功能之间进行连接 [英] How can I using hosting rewirtes to connect between firebase hosting and cloud functions

查看:100
本文介绍了如何使用托管轮播在Firebase托管和云功能之间进行连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于更具体描述的新内容

我正在尝试使用express和firebase函数进行高级路由.

I'm trying to use express and firebase function for advanced routing.

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

But I've got this error when I trying to get my firebase cloud function

TypeError:无法在/Users/fredriccliver/Projects/firebase-test/functions/node_modules/express/lib/router/index.js:635:15下读取未定义的属性"apply"(/Users/fredriccliver/在Function.handle(/Users/fredriccliver/Projects/firebase-test/functions/node_modules/express/lib/router/路由器(/Users/fredriccliver/Projects/firebase-test/functions/node_modules/express/lib/router/index.js:47:12)位于/usr/local/lib/node_modules/在/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:568:19在/usr/的Generator.next()处的firebase-tools/lib/emulator/functionsEmulatorRuntime.js:593:20 __awaiter处新Promise()处的local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:8:71(/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js: 4:12)

TypeError: Cannot read property 'apply' of undefined at /Users/fredriccliver/Projects/firebase-test/functions/node_modules/express/lib/router/index.js:635:15 at next (/Users/fredriccliver/Projects/firebase-test/functions/node_modules/express/lib/router/index.js:260:14) at Function.handle (/Users/fredriccliver/Projects/firebase-test/functions/node_modules/express/lib/router/index.js:174:3) at router (/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/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

/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

/functions/api.js

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

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

module.exports = router

有人可以给我一个解决这个问题的线索吗?

Could anyone give me a clue to solve this problem?

以前的内容

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

I'm trying to convert my node(with express) project into Firebase compatible one.

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

I add my API endpoint in /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时,我可以得到响应,但是那是行不通的.

So, my expectation was when I call localhost:5000/api or localhost:5000/api/sentences, I could get a response, but it doesn't work.

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

For that, I tried .onCall to submit my cloud function rather than using .onRequest.

但我致电functions.httpsCallable("endpoint")时,仅接受POST请求.

But only POST request was accepted when I call functions.httpsCallable("endpoint").

因此,我尝试使用onRequest并在firebase.json中托管中进行重写

So, I tried to use onRequest and rewrites in hosting in firebase.json

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

但是,我只是遇到了这个错误:

But, I just got this error:

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

TypeError: Cannot read property 'apply' of undefined at /Users/fredriccliver/Projects/Speech/functions/node_modules/express/lib/router/index.js:635:15 at next

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

How should I take the way to call my functions from frontend javascript?

推荐答案

检查了YouTube剪辑并尝试了更多内容后,我解决了问题.

I resolved after check a youtube clip and trying some more.

总结在我的中等职位上. https ://medium.com/@fredriccliver/how-to-use-an-express-router-within-the-cloud-function-developing-environment-cb64face4043

summarised on my medium post. 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"
    }]
  }
}

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

So, I could approach all endpoints

这篇关于如何使用托管轮播在Firebase托管和云功能之间进行连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆