I need to call a cloud function which itself is an express app via AngularFire. If I call a "normal" function like:
BACKEND
export const myTestFunction = functions.https.onCall((data, context) => {
return { msg: data.coolMsg.toUpperCase(), date: new Date().getTime() };
});
FRONTEND-ANGULAR
callCloudFunction() {
const callable = this.functions.httpsCallable('myTestFunction');
const obs = callable({ coolMsg: 'Hello backend' });
obs.subscribe(async res => {
console.log(res);
});
}
The above function gets executed properly. Now if the function is an express app, I want to call a route inside it for example: api/v1/user/profile , which will return the user profile data.
BACKEND
// main is the entire express app which you access as api/v1/<your_route>
export const api = functions.https.onRequest(main);
the express app is working properly. I have checked everything with postman.
So how should I call the route? is it possible or should I do it "manually"?
In this instance, you won't use the angularfire api to call your http function.
Use the HttpClientModule
, inject the HttpClient
service in your component constructor and call your function with an http request:
const headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
const url = `/api/v1/user/profile`;
return this.http.get(url, { headers });
Do not forget to rewrite your routes in your firebase.json to map your app and your express server to the same origin:
{
"target": "app",
"public": "/dist",
"rewrites": [
{
"source": "/api/v1/**", // ==> call cf for every request matching the source path
"function": "api"
}
]
}
To test it, use the firebase:emulator. It won't work with localhost using ng serve.