【问题标题】:Deploying Express server to Firebase issue将 Express 服务器部署到 Firebase 问题
【发布时间】:2020-10-07 18:09:13
【问题描述】:

我正在尝试将我的 express 服务器部署到 firebase 功能。当我尝试使用“firebase serve”命令运行它时,它运行良好,但是当我部署它时,我无法访问我的服务器的任何路由。

我的函数/index.js:

const functions = require('firebase-functions')
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const controller = require('./controller')
const cors = require('cors')

app.use(cors({ origin: true }))
app.use(bodyParser.json())
app.use(express.json())

app.get('/getGardens', controller.getGardens)
app.post('/getPresentDogsInGarden', controller.getPresentDogsInGarden)

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

firebase.json:

{
  "hosting": {
    "public": "public",
    "rewrites" : [{"source" : "**", "function" : "App"}]
  }
}

我的链接:...whatever/App/getGardens

得到响应:'错误:无法处理请求'

编辑:

解决了我的问题。代码和初始化一切正常。 我不知道的是,通过免费的 spark 计划,我无法使用不属于 google 的 API。我在我的控制器模块中使用了 mongo,但它无法连接。我升级到了 blaze plan,它运行良好。

【问题讨论】:

  • Firebase 函数控制台中的确切错误是什么?

标签: javascript node.js firebase express google-cloud-functions


【解决方案1】:

这是一个 Medium 帖子,展示了如何做到这一点:

https://codeburst.io/express-js-on-cloud-functions-for-firebase-86ed26f9144c

代码示例:

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

/* Express */
const app1 = express()
app1.get("*", (request, response) => {
  response.send("Hello from Express on Firebase!")
})

const api1 = functions.https.onRequest(app1)

module.exports = {
  api1
}

【讨论】:

    【解决方案2】:

    我认为您的项目初始化有问题,如果您要部署到函数服务,.json 文件应该如下所示:

    {
      "firestore": {
          "rules": "firestore.rules",
          "indexes": "firestore.indexes.json"
       }
    }
    

    据我所见,您正在使用Hosting 服务,您应该将其更改为函数。现在,您要做的是将快速代码部署到托管服务,而不是将其部署到功能服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 2021-08-07
      • 2021-06-22
      • 1970-01-01
      • 2019-09-26
      • 2019-04-30
      • 1970-01-01
      相关资源
      最近更新 更多