简体   繁体   English

将 Express 服务器部署到 Firebase 问题

[英]Deploying Express server to Firebase issue

I'm trying to deploy my express server to firebase functions.我正在尝试将我的快速服务器部署到 firebase 函数。 when I try running it with 'firebase serve' command, it runs fine but when I deploy it I cant access any route of my server.当我尝试使用“firebase serve”命令运行它时,它运行良好,但是当我部署它时,我无法访问我的服务器的任何路由。

my functions/index.js:我的函数/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: firebase.json:

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

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

getting in response: 'Error: could not handle the request'得到回应:'错误:无法处理请求'

EDIT:编辑:

solved my problem.解决了我的问题。 everything was ok with the code and initialization.代码和初始化一切正常。 what I didn't know was that with the free spark plan I cannot use APIs that don't belong to google.我不知道的是,通过免费的 spark 计划,我无法使用不属于 google 的 API。 I was using mongo in my controller module and it failed to connect.我在我的 controller 模块中使用了 mongo,但它无法连接。 I upgraded to blaze plan and it worked fine.我升级到了 blaze 计划,它运行良好。

I think that you have something wrong with the project initialization, the.json file should look like this if you're deploying to the functions services:我认为您的项目初始化有问题,如果您要部署到功能服务,.json 文件应该如下所示:

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

And from what i'm seeing you're using the Hosting services, you should change it to functions.从我所看到的您正在使用Hosting服务的情况来看,您应该将其更改为功能。 Right now, what you're trying to do is to deploy you're express code to the hosting service, instead it should be deployed to the functions services.现在,您要做的是将您的快速代码部署到托管服务,而不是将其部署到功能服务。

Here is a Medium post that shows how to do this:这是一篇 Medium 帖子,展示了如何执行此操作:

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

Code example:代码示例:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将快速服务器代码部署为反应应用程序的 Firebase 云功能 - Deploying express server code as firebase cloud function for react app 在 firebase 上部署 react 前端和 express/nodejs 后端 - Deploying react frontend & express/nodejs backend on firebase Express Server路由问题 - Express Server routing issue 使用 NodeJS 后端(使用 express)为 heroku 上的服务器/数据库部署 ReactJS 应用程序 - Deploying ReactJS app with NodeJS backend (using express) for server/database on heroku 在 apache 服务器上部署 React (3000) 和 Express (8000) 应用程序 - Deploying React (3000) and Express (8000) app on the apache server Firebase存储在Express Server Environment中不起作用 - Firebase Storage Not working in Express Server Environment 使用 bitbucket 管道在 Firebase 托管中部署 Angular 应用程序的问题 - Issue in deploying Angular app in firebase hosting using bitbucket pipelines 部署 React + NodeJS + Express + MySQL 应用到 Heroku 只部署服务器而不是 React - Deploying a React + NodeJS + Express + MySQL App to Heroku only deploying the Server Not the React 带有护照的Express NodeJS服务器中的sureAdmin问题 - ensureAdmin issue in an Express NodeJS server with passport MIME 类型 ('text/html') 的 Express 服务器问题 - Express Server issue with MIME type ('text/html')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM