Firebase函数无法识别index.ts之外的Express应用程序路由。

huangapple go评论85阅读模式
英文:

Firebase functions not recognizing express app routes outside of index.ts

问题

我最近将一个TypeScript项目部署到了firebase-functions,并且所有代码都在index.ts文件中。一切都正常运行,但当我开始重构代码时,我很快意识到firebase无法识别在index.ts之外的文件中指定的路由(或者在你将express应用程序包装在functions.https.onRequest()中的文件)。

我在下面重现了这个问题:

// index.ts
import * as functions from 'firebase-functions'; 
import * as express from "express";

export const expressApp = express();

expressApp.get("/hi", (req, res) => {
   res.send("hi");
})

export const TestApp = functions.https.onRequest(expressApp);

在同一目录中的外部文件中添加了"/hello"路由。

// external.ts
import { expressApp } from "./index";

expressApp.get("/hello", (req, res) => {
    res.send("hello")
})

在我使用firebase-deploy后,index.ts中的"/hi"路由能够工作,但external.ts中的"/hello"路由却不能(报错Cannot GET /hello)。

我猜测TestApp在被任何外部文件调用之前被导出到firebase-functions中。

在TypeScript项目中有没有解决这个问题的方法?

英文:

I recently deployed a TypeScript project to firebase-functions with everything in index.ts. Everything worked but when I started to refactor my code I soon realized firebase does not recognize routes specified in files outside of index.ts (or the file where you wrap your express app in functions.https.onRequest()).

I have replicated the issue below:

//index.ts    
import * as functions from 'firebase-functions'; 
import * as express from "express";

export const expressApp = express();

expressApp.get("/hi", (req, res)=> {
   res.send("hi");
})

export const TestApp = functions.https.onRequest(expressApp);

External file in same directory that adds the "/hello" route.

//external.ts
import { expressApp } from "./index";

expressApp.get("/hello", (req, res)=> {
    res.send("hello")
})

After I use firebase-deploy, the "/hi" route in index.ts works but the "/hello" route in external.ts does not (Cannot GET /hello).

My guess is that the TestApp is being exported to firebase-functions before it's called by any external files.

Is there any way to get around this in a TypeScript project?

答案1

得分: 2

以下是已翻译的内容:

当您启动项目时,将执行index.ts内部的代码,但由于它根本没有引用external.ts,因此该代码永远不会执行。

您可以改为从index.ts内部调用external.ts,然后通过传递您想要修改的express应用程序来调用它。

//index.ts    
import * as functions from 'firebase-functions'; 
import * as express from "express";
import { attachRoutes as attachExternalRoutes } from "./external";

export const expressApp = express();

expressApp.get("/hi", (req, res) => {
   res.send("hi");
})

attachExternalRoutes(expressApp); // attaches routes in external

export const TestApp = functions.https.onRequest(expressApp);
//external.ts
export function attachRoutes(expressApp) {
  expressApp.get("/hello", (req, res) => {
    res.send("hello")
  })
}
英文:

When you start your project, the code inside index.ts is executed, but as it doesn't reference external.ts at all, that code is never executed.

What you can do instead is call external.ts from inside index.ts and then invoke it by passing in the express app you would like to modify.

//index.ts    
import * as functions from 'firebase-functions'; 
import * as express from "express";
import {attachRoutes: attachExternalRoutes} from "./external";

export const expressApp = express();

expressApp.get("/hi", (req, res)=> {
   res.send("hi");
})

attachExternalRoutes(expressApp); // attaches routes in external

export const TestApp = functions.https.onRequest(expressApp);
//external.ts
export function attachRoutes(expressApp) {
  expressApp.get("/hello", (req, res)=> {
    res.send("hello")
  })
}

huangapple
  • 本文由 发表于 2020年1月3日 20:14:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定