【问题标题】:Custom domain Firebase cloudfuntions API自定义域 Firebase cloudfunctions API
【发布时间】:2020-12-02 17:07:57
【问题描述】:

我已经使用 Firebase Cloud Functions 生产了一个 Express API,但我需要向它添加自定义域。我跟着 these instructions 使用 Firebase 托管为 Cloud Functions 创建了一个自定义域,最终得到了以下 firebase.json

{
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ],
    "source": "functions"
  },
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "emulators": {
    "functions": {
      "port": 5001
    },
    "firestore": {
      "port": 8080
    },
    "ui": {
      "enabled": false
    }
  },
  "hosting": [{
    "site": "devbp",
    "target:":"devbp",
    "public": "public",
    "rewrites": [
      {
        "source": "/api/**",
        "function": "api"
      }
    ]
  }]
}

index.ts

import { functions } from './utils/exports-converter';
import App from './web-api/configs/app';

export * from './functions';
export * from './backup/schedules';
export * from './firestore-triggers/credits/credits-triggers'
export * from './schedules/transactions/aggregations-schedules';


export const api = functions.runWith({
    memory: '1GB',
    timeoutSeconds: 300
}).https.onRequest(App.express);

exports-converter.ts

import * as express from "express";
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";

export { express, functions, admin };

app.ts

import * as cors from "cors";
import * as bodyParser from "body-parser";
import Routes from "./routes";
import { express } from "../../utils/exports-converter";

class App {
  public express: express.Application;
  constructor() {
    this.express = express();
    this.init();
    this.mountRoutes();
  }
  private init() {
    const corsOptions = { origin: true };
    this.express.use(cors(corsOptions));
    this.express.use(bodyParser.raw());
    this.express.use(bodyParser.json());
    this.express.use(bodyParser.urlencoded({ extended: true }));
  }
  private mountRoutes() {
    Routes.mount(this.express);
  }
}

问题在于 api 内的端点不可访问,例如GET devbp.web.app/api/user?id=123 返回 Cannot GET /api/user。此响应表明 Express 正在处理请求(如果没有,Hosting 将抛出 404 页面)但未按预期处理。

我相信我在firebase.json 中遗漏了一些东西,因为如上所述,相同的 api 目前正在生产中。

有什么想法吗?

【问题讨论】:

  • 请编辑问题以同时显示未按您期望的方式工作的功能代码。我们应该有足够的信息来复制您正在观察的内容。
  • @DougStevenson 添加了更多文件,如果您需要其他文件,请告诉我。谢谢
  • 我看不到您在哪里定义了应按预期处理请求的快速路由。我强烈建议将其归结为minimal code sample,尽可能多地删除不必要的代码。

标签: javascript firebase google-cloud-platform google-cloud-functions firebase-hosting


【解决方案1】:

我不确定,如果我遵循逻辑,但是我在代码中的任何地方都看不到任何new App。如果我理解正确,如果您不创建新的 App 对象,您将不会运行它的构造函数,因此不会创建快速应用程序。

我认为的第二件事 :) 是,再次根据我的理解,当 http GET 请求调用 GET 请求时应该调用 Express get() 方法。

我发现这个tutorial不是云功能,但逻辑相似。创建新实例 App 并调用 get 方法。

【讨论】:

    【解决方案2】:

    根据documentationhere 也是如此)托管语法是:

    "hosting": {
    ...
    }
    

    在呈现的firebase.json 中,您拥有[{ ... }]。 尝试删除方括号。

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 2019-12-08
      • 1970-01-01
      • 2018-07-26
      • 2020-11-04
      • 2017-08-09
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多