我能';在Firebase主机上快速访问

我能';在Firebase主机上快速访问,firebase,firebase-hosting,Firebase,Firebase Hosting,现在,我通过节点服务器创建了一个文件,如 const functions = require("firebase-functions") const express = require("express") /* Express */ const app = express() app.get("/test", (request, response) => { response.send("Hello from Express on Firebase!") }) const api

现在,我通过节点服务器创建了一个文件,如

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

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

const api1 = functions.https.onRequest(app)


module.exports = {
  api1
}
firebase.json

    {
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ]
  }
}
我已经将它部署到firebase主机上,并尝试在firebase主机上访问它。index.html向我显示,但当我需要
/test
时,它将返回404.html页面!我错过了什么

更新firebase.json并将重写添加到

现在的信息不同了


答案
我必须将我的项目文件结构化以 -项目(文件夹) ---函数(文件夹)(包含所有nodejs文件)
---public(filder)

您需要在firebase.json文件中包含一个
重写
部分。它告诉Firebase服务器如何路由传入的任何请求。。。现在,你什么都没说

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

这实际上并不是对您问题的回答,但它演示了使用cloud function express应用程序设置重写的正确方法。

请将您的firebase.json文件包括在内好吗?你忘了设置重写吗?@JeremyW谢谢你的关注,我添加了一个firebase.json文件,如果我有几个路由,比如const users=require('./routes/users');const books=需要(“./路线/书籍”);应用程序使用('/users',users);应用程序使用('/books',books);在我添加重写后,消息变得不同“404.这是一个错误。在该服务器上找不到请求的URL。这就是我们所知道的。”@MahmoudNiypoo您的意思是,在添加重写后,您访问
/test?
/
?那你拿到404了吗?除了
/test
之外的任何其他路由都将是404,因为它没有在Express实例中配置。如果您有其他路由,那么您只需将它们包括在express设置中,就像任何其他node.js项目一样。此时,重写会将所有请求(双astrik)发送到您的express应用程序。此时,我只有一条路由
/test
,这是在调用root
/
时发生的。index.html对男性显示,但在调用`/test'时,我在顶部添加的消息对我显示404。那是个错误。在此服务器上找不到请求的URL。我们就这些know@MahmoudNiypoo嗯,这对我来说没有任何意义-在你的重写部分,
/test
应该显示你的Hello消息。
"hosting": {
    "rewrites": [
        {
            "source": "**",
            "function": "api1"
        }
    ]
}