[英]Nuxt3 and Firebase Cloud Functions: Where to place Firebase cloud functions in the /server directory?
我成功地能夠使用 Firebase 雲功能將我的 Nuxt 3 應用程序部署到 Firebase 主機。 現在,我想創建另一個 Firebase 雲 function 自動運行一些后端代碼以響應由 Firebase Firestore 數據庫觸發的事件。
作為測試,我想添加以下簡單的“Hello World” Http function 作為測試:
server/api/functions/helloWorld.js
:
import * as functions from 'firebase-functions'
export default defineEventHandler(() => {
return functions.https.onRequest((request, response) => {
console.log(request)
return response.send('Hello from Firebase!')
})
})
我運行npm run build
並在.output/server/chunks
中看到文件。 然后,我運行 firebase 模擬器進行測試:我輸入了http://localhost:5001/<myprojectid>/us-central1/server/api/functions/helloWorld
但得到以下服務器錯誤:
{"url":"/api/functions/helloWorld","statusCode":404,"statusMessage":"Not Found","message":"Not Found","description":""}
但是,當我嘗試訪問其他功能時,我沒有問題(即/server/api/posts/all
):
這是“工作” function(不是 Http 雲 Function,雖然)的組成:
/server/api/posts/all.ts
:
import { firestore } from '@/server/utils/firebase'
export default defineEventHandler(async (event) => {
const colRef = firestore.collection('posts').orderBy('createdAt', 'desc')
const querySnapshot = await colRef.get()
const posts = []
querySnapshot.forEach((doc) => {
if (doc.data().public_id) // ensure we only show posts with images
posts.push(doc.data())
})
return {
posts
}
})
如何訪問 Firebase Function ( helloWorld
)?
這是我的firebase.json
文件:
{
"functions": {
"source": ".output/server"
},
"hosting": [
{
"site": "<removed>",
"public": ".output/public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"cleanUrls": true,
"rewrites": [
{
"source": "**",
"function": "server"
}
]
}
]
}
使用 firebase 預設時,nuxt3 僅將函數視為渲染器。 這是推薦的方式How to add additional firebase functions when deploy nuxt to firebase或者你可以編寫腳本復制文件並結合index.js,並在預部署時運行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.