【问题标题】:Firebase-hosting can't get to api routes nodeFirebase 托管无法访问 api 路由节点
【发布时间】:2019-10-15 11:44:31
【问题描述】:

我有一个 '/' 的路由,但它提供公共 html 索引文件,我尝试将我的 api 的根目录更改为 /api/**,但它给了我 404 错误。 当我在本地运行它时它运行良好,但是当我部署它时,我遇到了只有 index.html 文件的问题。 这是 firebase.json 文件

{
  "hosting": {
    "public": "public",
    "rewrites":[
      {
        "source":"/api/**",
        "functions":"app"
      }
    ],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

index.js 文件夹

const functions = require('firebase-functions')
const express = require('express')
\\.
\\.
\\requiring routes and middleware
app.use(helmet())
app.use(express.json())
app.use(cookieParser())
app.use(cors({
    origin:"*",
    credentials:true
}))

app.get('/',(req,res)=>{
    res.status(200).send("works");
})
app.use(locationActions)
app.use(filterActions)
app.use('/auth',userAuth)
app.use('/adm',AdminRoutes)
app.use('/prod',prodActions)
app.use('/user',userActions)


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

【问题讨论】:

  • 您访问的哪个 URL 不能按您预期的方式工作?您现在配置的内容只会将路径前缀为“/api”的 URL 重写为您的函数。
  • uiniquebackend.firebaseapp.com/api .. 我希望这能给我工作信息,因为我在 app.js 文件中有这一行 res.status(200).send("works");
  • --edit--- 我将 '/' 更改为 '/api/test/' app.get('/',(req,res)=>{ res.status(200).send("works"); }) 仍然给我找不到 404 错误

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


【解决方案1】:

.send() 似乎不能很好地与 firebase 一起工作,我只需要说一些类似 response.json({message}) 的话,这实际上会发送响应。 mongoose 连接也被禁止,因为我使用的是免费帐户,因此 Firebase 之外的任何连接都被拒绝。

【讨论】:

    猜你喜欢
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 2018-09-25
    相关资源
    最近更新 更多