You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求/up路由持续返回CANNOT GET /up错误的排查求助

问题:请求/up路由返回CANNOT GET /up错误

我尝试请求/up路由获取响应,但持续收到CANNOT GET /up错误,其他路由(如/、/signup)均可正常响应。我已尝试调整/up路由的定义位置,但问题仍未解决。

路由配置文件routes_config.ts:

import {Application} from "express";
import {AuthenticationService} from
  "../authentication/application/authentication_repository";
import {TYPES} from "../core/types";
import {myContainer} from "../core/inversify_config";

export function routesConfig(app: Application) {
  app.get("/", (_req, res) => res.status(200).send("Hey there!"));

  app.post("/signup", async (req, res) => {
    try {
      const {email, password} = req.body;
      if (password.toLowerCase == null || email.toLowerCase == null) {
        return res.status(400).send({message: "Missing fields"});
      }
      const authService = myContainer
          .get<AuthenticationService>(TYPES.AuthenticationService);
      console.log("after call the get injections");
      const uid = await authService.signUp(email, password);
      return res.status(200).send({uid});
    } catch (error) {
      return res.status(400).send(error);
    }
  }
  );

  app.get("/up", (_req, res) => res.status(200).send("Hey!"));
}

入口文件index.ts:

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import * as express from "express";
import * as cors from "cors";
import * as bodyParser from "body-parser";
import {routesConfig} from "./routes_config/routes_config";

const app = express();
admin.initializeApp(functions.config().functions);
admin.firestore();

app.use(bodyParser.json());
app.use(express.urlencoded({extended: false}));
app.use(cors({origin: true}));

routesConfig(app);

export const api = functions.https.onRequest(app);
解决方案

问题出在Firebase Cloud Functions的路由挂载逻辑上:当你把Express应用通过functions.https.onRequest(app)导出为名为api的函数时,所有Express路由都会被自动挂载到函数的URL路径下。也就是说,你实际需要请求的路径是/api/up,而非直接/up。

验证方式:

  • 访问/api会返回Hey there!(对应Express根路由/)
  • 访问/api/signup可正常处理请求
  • 访问/api/up就能得到预期的Hey!响应

如果希望保持/up这类短路径访问,可以修改Express配置给所有路由统一添加前缀:

// 在index.ts中调用routesConfig前添加
const apiRouter = express.Router();
routesConfig(apiRouter);
app.use('/api', apiRouter);

不过更直接的方式是直接使用/api/up作为请求路径,这是Firebase Functions的默认行为。

内容的提问来源于stack exchange,提问作者Gbenga B Ayannuga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 16:52:40