【发布时间】: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