首页 > 解决方案 > Know if express app is running as firebase cloud function

问题描述

I have an express app that I want to run locally as a standalone server in my machine however, when deploying it to firebase cloud functions I need to set it as a cloud function.

Is there a reliable way to know in which environment the app is running without manually setting env variables or what is the best practice?

eg:

if(isRunningInFirebase()){
  exports.myFun=functions.https.onRequest(app)
} else app.listen(3030)

标签: node.jsfirebaseexpressgoogle-cloud-functions

解决方案


There are environment variables that are automatically populated in the functions runtime and in locally emulated functions, as documented here. One of them for example is the GCLOUD_PROJECT variable, which is set to your Firebase project ID. You can have your app checking for it like this:

if(process.env.GCLOUD_PROJECT) { 
    // running in Firebase environment 
}
else { 
    // running somewhere else 
}

推荐阅读