Firebase functions are having trouble identifying the routes in the express app that are located outside of the

Recently, I faced an issue while deploying a TypeScript project to firebase-functions where all the code was stored in index.ts. Initially, everything worked fine but when I decided to refactor my code, I discovered that firebase was not able to recognize routes specified in files outside of index.ts (or the file containing functions.https.onRequest()).

Below is a reproduction of the problem:

//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);

An external file in the same directory adding the "/hello" route.

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

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

After deploying with firebase-deploy, the "/hi" route in index.ts works correctly, but the "/hello" route in external.ts returns an error (Cannot GET /hello).

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

Is there a workaround for this issue in a TypeScript project?

Answer №1

At the beginning of your project, the code within index.ts is executed. However, because it does not make any reference to external.ts, the code in that file remains untouched and never runs.

To remedy this, you can call upon external.ts from within index.ts and then activate it by passing in the express app that you wish 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")
  })
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve the value of an object without relying on hardcoded index values in TypeScript

I am working with an object structure retrieved from an API response. I need to extract various attributes from the data, which is nested within another object. Can someone assist me in achieving this in a cleaner way without relying on hardcoded indices? ...

What improvements can I make to enhance my method?

I have a block of code that I'm looking to clean up and streamline for better efficiency. My main goal is to remove the multiple return statements within the method. Any suggestions on how I might refactor this code? Are there any design patterns th ...

Capture data from a Telegram bot and store it in a Google Sheet

I am trying to use a spreadsheet through a Telegram bot as a TODO list so that when I input something on my phone, it is saved in the spreadsheet. (I'm following this tutorial https://www.youtube.com/watch?v=XoTpdxbkGGk, which seems accurate with Goog ...

Accessing the specific data I require is proving to be a challenge on Express-Gateway

Great job team! I successfully launched my Node JS project on PORT 8001 http://localhost:8001/companies_getAll (returns data) I'm planning to implement a microservice architecture using EXPRESS-GATEWAY. However, despite configuring the settings belo ...

Running several ReactJS applications - Implementing Node.js with Express to serve them

I'm currently facing a challenge in serving two separate Reactjs applications using Express. App1 *Primary app: showcases different data sets. Main front-end *path: "/" (localhost:5000/) App2 *Secondary App: User Dashboard, displaying specific us ...

The issue arises when using multiple route files in Route.js, as it hinders the ability to incorporate additional functions within the

After breaking down Route.js into multiple controllers, I'm stuck on why I can't add an extra function to block permissions for viewing the page. // route.js module.exports = function(app, passport) { app.use('/profile&apos ...

The error message "this.startLoginAnimatioon is not defined as a function" popped up

I've been developing a login system using TypeScript but I keep encountering an error that I can't figure out. Here's the issue in detail: https://i.sstatic.net/PN4N8.png The problem arises when the this.startLoginAnimation() function ...

Is it possible to refresh the webpage in Angular when the tab is clicked?

Can someone help me find a solution to reload an Angular app's page when the user selects the browser tab? I've been exploring using window.location.reload() for this purpose, but I need guidance on triggering it specifically when the tab is sel ...

Once the Angular project has been initialized, the Button disable attribute cannot be modified

In my Ionic-Angular project, I am creating registration pages where users input their information in multiple steps. For each step, there is a button that remains disabled until the correct information is entered. An issue arises when transitioning to the ...

Updating the junction table in a many-to-many relationship with Sequelize

I am facing a challenge in updating a junction table with multiple ids. My goal is to update the junction table by removing rows with missing ids and creating new ones. Here is my current setup: ActorFilm.belongsTo(models.Actor, { foreignKey: 'acto ...

Tips for packaging an "express" application built on node.js with the help of Webpack

Seeking to package a node.js express application into a single file for distribution, I've turned to webpack. The bundling process seems to be successful, but upon attempting to run the app from the bundle, an error surfaces: Error: secure random n ...

"Discover the ideal length for your secret key with ease

Is there a recommended secret key length when cryptographically hashing session data in connect/express? I've seen suggestions ranging from 60+ characters to the classic 'keyboard cat'. Considering my current setup utilizes SSL for the enti ...

Encountering an error with node.js server.listen function on an AWS Ubuntu server

Struggling to execute a basic node.js file on an AWS server running Ubuntu 14.04 and Apache 2.4.7 var http = require('http'); var hostname = '33.33.33.33'; var port = 3000; var server = http.createServer(function(req, res) { cons ...

Guide on toggling mat-checkbox according to API feedback in Angular 6

Just starting out with angular 6 and I'm attempting to toggle the mat-checkbox based on the API response. However, I seem to be having trouble. All the checkboxes are showing as checked even when the API response is false. <div class="col-sm-12" ...

Is it possible to utilize both $uibModal and $uibModalInstance within the same controller to create a modal popup in an Angular project incorporating TypeScript?

Being new to Angular with Typescript, I encountered an issue while trying to implement a modal popup in Angular. The problem arises when I have a dropdown menu that triggers the opening of a modal popup with two buttons, "Yes" and "No". To handle this, I h ...

Building a Modular Socket.io System using Express 4

I'm currently working on modularizing my application files, and I've encountered a challenge with the integration of Socket.io. My goal is to utilize io within my routes.js file. Here's an example of what I'm attempting: var router = r ...

When a React component in TypeScript is passed as a parameter and then assigned to a variable, an error with code TS2604 may occur stating that the JSX element type does not

I am currently facing an issue with handling props of a passed React Element in my Factory. I am getting a TypeScript error that says: TS2604: JSX element type 'this.extraBlock' does not have any construct or call signatures. This is my Child co ...

Definition of Promise resolve type in Visual Code's d.ts file

Need help with: // api.js export function getLayout(){ return axios.get('/api/layout').then(res => res.data) } // api.d.ts declare interface JSONResponse { meta: object, data: Array<Field> } export declare function getLayout ...

Prompt: "Choosing between Dialogflow fulfillment library and Express for handling responses, what's your pick?"

Currently, I'm attempting to integrate the dialog-fulfillment-library with Express without relying on Firebase functions. However, I'm encountering difficulties when it comes to responding using the agent object. const { WebhookClient, Card, ...

How can you merge one object with two different mongoose models?

Consider the scenario where I have two mongoose models set up: User Model Business Favorite Model Currently, I'm successfully retrieving the combined result if a user has any favorite businesses. However, I suspect that my current implementation mi ...