如何调用作为快速应用程序的云功能?

How to call a cloud function which is an express app?

我需要通过 AngularFire 调用一个云函数,它本身就是一个快速应用程序。如果我调用一个“普通”函数,例如:

后端

export const myTestFunction = functions.https.onCall((data, context) => {
  return { msg: data.coolMsg.toUpperCase(), date: new Date().getTime() };
});

前端-ANGULAR

callCloudFunction() {
      const callable = this.functions.httpsCallable('myTestFunction');
      const obs = callable({ coolMsg: 'Hello backend' });
      obs.subscribe(async res => {
           console.log(res);
      });
  }

以上函数正确执行。现在,如果该功能是一个快速应用程序,我想在其中调用一个路由,例如:api/v1/user/profile,这将 return 用户个人资料数据。

后端

// main is the entire express app which you access as api/v1/<your_route>
export const api = functions.https.onRequest(main);

快递应用程序正常运行。我已经和邮递员核对过了。

那么路由应该怎么调用呢?有可能还是我应该“手动”做?

在这种情况下,您不会使用 angularfire api 来调用您的 http 函数。
使用 HttpClientModule,在您的组件构造函数中注入 HttpClient 服务并使用 http 请求调用您的函数:

    const headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    const url = `/api/v1/user/profile`;
    return this.http.get(url, { headers });

不要忘记在您的 firebase.json 中重写您的路由,以将您的应用程序和您的快速服务器映射到同一来源:

{
  "target": "app",
  "public": "/dist",
  "rewrites": [
     {
       "source": "/api/v1/**",  // ==> call cf for every request matching the source path
       "function": "api"
     }
  ]
}

要测试它,请使用 firebase:emulator。它不会使用 ng serve 与 localhost 一起工作。