node.js - 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)
解决方案
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
}
推荐阅读
- python - 包含默认 ID 作为参数不起作用
- ruby-on-rails - 区分链接以根据所选链接更改一页
- clang - 如何使用“clang”启用“cfi”为“aarch64”编译“.c”文件?
- php - 从单击的表行中获取 id 并显示该行的值
- php - WordPress 按类别名称获取帖子不起作用
- javascript - axios 调用,使用 vue 将响应数据获取到选择选项中
- unit-testing - 测试 .Net Core Identity GetUserAsync
- c++ - 分支预测是否仍在显着加快数组处理速度?
- python - 使用 Azure Key Vault 和 Active Directory 检索机密
- ansible - 无法在 Debian 伸展的 Ansible 2.7 上使用 NPM 包安装 PM2