【问题标题】:Express app using firebase functions使用 firebase 函数的 Express 应用
【发布时间】:2018-10-31 16:49:59
【问题描述】:

我正在尝试在 firebase 中运行 Express 应用程序,我正在按照这个官方视频的步骤进行操作:

Node.js apps on Firebase hosting crash course

我的 Express 应用实际上是在这个 URL 上运行的

http://localhost:5001/my-app/us-central1/app/test

但是在视频中,演示是在这个 url 上运行的

http://localhost:5000/test

所以,我真的很困惑,我已经按照教程显示的那样做了一切

这是我在函数/index.js 上的代码

const functions = require('firebase-functions');

const express = require('express')
const app = express()

app.get('/test', (req, res) => {
    res.send(`Well if i'm reading this the app is working`)
})


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

这是 firebase.json

{
  "hosting": {
    "public": "public",
    "rewrite":[{
      "source": "/test",
      "function": "app"
    }],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

我做错了什么?如果我输入 localhost:5000 我只是得到公共静态 index.html,我只想自己控制应用程序。

希望你能给我一点帮助,让我记录下来,谢谢!

【问题讨论】:

  • firebase.json/test 映射到您的应用,因此 localhost:5000/test 应该服务于 express 应用。
  • 这是合乎逻辑的行为,但不是以这种方式工作,而是呈现 public/index.html 文件
  • 你是如何在本地运行函数的?
  • 这是由控制台打印的:functions:准备模拟函数。警告:您使用的是 Node.js v8.11.2,但 Google Cloud Functions 仅使用 6.11.1。 i 托管:从以下位置提供托管文件:公共 + 托管:本地服务器:localhost:5000 + 功能:应用程序:localhost:5001/r-commerce/us-central1/app
  • 所以看起来你的应用在本地是来自localhost:5001/r-commerce/us-central1/app的服务器。如果您打开该 URL,会发生什么。

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


【解决方案1】:

这里的问题是 Firebase 托管将使用任何重写提供与 之前 的任何路径匹配的静态内容。如果要使用 Cloud Functions 控制路径,则必须确保没有与路径匹配的静态内容。

特别是对于单页应用,删除 index.html 至关重要,因为它将始终作为静态内容提供。

【讨论】:

    猜你喜欢
    • 2021-03-17
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2021-08-11
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多