在部署我的功能和托管方面存在差异。
问题是,我已经让他们在独立的分支机构上独立工作。。当尝试集成托管和我的云功能时,我的云函数似乎没有部署。我的终端中没有收到任何错误,当我在firebase控制台中单击"函数"时,它是默认屏幕,就好像没有部署任何函数一样。
这是我的firebase.json,用于托管+功能部署主机在这里工作,但功能没有部署
{
"hosting": {
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
},
{
"source": "/api/v1/**",
"function": "webApi"
}
]
},
"functions": {
"predeploy": [
"npm --prefix "$RESOURCE_DIR" run lint"
],
"source": "functions"
}
}
这是我的firebase.json,只有功能,没有托管
{
"hosting": {
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/api/v1/**",
"function": "webApi"
}
]
},
"functions": {
"predeploy": [
"npm --prefix "$RESOURCE_DIR" run lint"
],
"source": "functions"
}
}
下面是我的函数/index.js
const functions = require('firebase-functions')
const admin = require('firebase-admin')
const serviceAccount = require('./serviceAccount.json')
const express = require('express')
const bodyParser = require('body-parser')
const _ = require('lodash')
const { getObjectValues } = require('./helper-functions.js')
const json2csv = require('json2csv').parse
admin.initializeApp({
...,
})
const db = admin.firestore()
const app = express()
const main = express()
main.use('/api/v1', app)
main.use(bodyParser.json())
exports.webApi = functions.https.onRequest(main)
app.get('/test', (request, response) => {
response.send('API TEST')
})
app.get('/surveys', (request, response) => {
const surveyCollection = db.collection('/surveys')
return (
surveyCollection
.get()
// eslint-disable-next-line promise/always-return
.then(querySnapshot => {
let surveyList = []
querySnapshot.forEach(doc => {
const survey = doc.data()
surveyList.push(survey)
})
response.send(surveyList)
})
)
})
app.get('/surveys/:survey', (request, response) => {
const surveyId = request.params.survey
const userAnswers = db.collection(`/surveys/${surveyId}/submissions`)
return (
userAnswers
.get()
// eslint-disable-next-line promise/always-return
.then(querySnapshot => {
let surveySubmissions = []
querySnapshot.forEach(doc => {
const userSubmission = doc.data()
surveySubmissions.push({
..._.mapValues(userSubmission.answers, getObjectValues), // format answers
...userSubmission.anonUser,
})
})
response.setHeader('Content-disposition', 'attachment; filename=cna.json')
response.set('Content-Type', 'application/json')
response.status(200).send(surveySubmissions)
})
.catch(error => {
console.log(error)
})
)
})
主机+功能分支,我键入"firebase deploy">
terminal output:
i deploying functions, hosting
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions@ lint surveyplus-cnafunctions
> eslint .
+ functions: Finished running predeploy script.
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (82.32 KB) for uploading
+ functions: functions folder uploaded successfully
i hosting[surveyplus-effd5]: beginning deploy...
i hosting[surveyplus-effd5]: found 30 files in build
+ hosting[surveyplus-effd5]: file upload complete
i functions: updating Node.js 8 function webApi(us-central1)...
+ functions[webApi(us-central1)]: Successful update operation.
i hosting[surveyplus-effd5]: finalizing version...
+ hosting[surveyplus-effd5]: version finalized
i hosting[surveyplus-effd5]: releasing new version...
+ hosting[surveyplus-effd5]: release complete
+ Deploy complete!
仅云上功能分支firebase deploy
输出
=== Deploying to '...'...
i deploying functions, hosting
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions@ lint surveyplus-cnafunctions
> eslint .
+ functions: Finished running predeploy script.
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (82.33 KB) for uploading
+ functions: functions folder uploaded successfully
i hosting[surveyplus-effd5]: beginning deploy...
i hosting[surveyplus-effd5]: found 30 files in build
+ hosting[surveyplus-effd5]: file upload complete
i functions: updating Node.js 8 function webApi(us-central1)...
+ functions[webApi(us-central1)]: Successful update operation.
i hosting[surveyplus-effd5]: finalizing version...
+ hosting[surveyplus-effd5]: version finalized
i hosting[surveyplus-effd5]: releasing new version...
+ hosting[surveyplus-effd5]: release complete
+ Deploy complete!
您的云函数已经正确部署,因为我可以直接调用/api/v1/test
路由(在撰写本文时(。
然而,在您的firebase.json
文件中,您的重写顺序是不正确的,如文档中突出显示的:
重要信息:在
rewrites
属性中,Hosting响应将遵守捕获请求路径的第一个source
glob指定的规则。
{
"hosting": {
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/api/v1/**",
"function": "webApi"
},
{
"source": "**",
"destination": "/index.html"
}
]
},
"functions": {
"predeploy": [
"npm --prefix "$RESOURCE_DIR" run lint"
],
"source": "functions"
}
}
因此,/api/v1/test
调用没有发送给云功能处理,而是被错误地发送到了React应用程序。交换订单应该可以解决这个问题。
另一个注意事项是,如果您打算将所有调用格式化并解析为JSON,则应该对main.use
调用进行重新排序。我也会使用一个express Router,而不是一个完整的express()
实例。
const main = express(); // full express instance
const app = express.Router(); // "child" express instance
main.use(bodyParser.json()) // parse body as json, then hand over to app
main.use('/api/v1', app)