[英]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.