【发布时间】: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/api 或localhost: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