Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 了解express应用程序是否作为firebase云功能运行_Node.js_Firebase_Express_Google Cloud Functions - Fatal编程技术网

Node.js 了解express应用程序是否作为firebase云功能运行

Node.js 了解express应用程序是否作为firebase云功能运行,node.js,firebase,express,google-cloud-functions,Node.js,Firebase,Express,Google Cloud Functions,我有一个express应用程序,我想在我的机器上作为独立服务器在本地运行,但是,当将其部署到firebase云功能时,我需要将其设置为云功能 有没有可靠的方法可以知道应用程序在哪个环境下运行,而无需手动设置环境变量,或者最佳做法是什么 例如: 有一些环境变量是在函数运行时和本地模拟函数中自动填充的,如文档所示。例如,其中一个变量是GCLOUD\u PROJECT变量,该变量设置为您的Firebase项目ID。您可以让您的应用程序按如下方式进行检查: if(process.env.GCLOUD_P

我有一个express应用程序,我想在我的机器上作为独立服务器在本地运行,但是,当将其部署到firebase云功能时,我需要将其设置为云功能

有没有可靠的方法可以知道应用程序在哪个环境下运行,而无需手动设置环境变量,或者最佳做法是什么

例如:


有一些环境变量是在函数运行时和本地模拟函数中自动填充的,如文档所示。例如,其中一个变量是
GCLOUD\u PROJECT
变量,该变量设置为您的Firebase项目ID。您可以让您的应用程序按如下方式进行检查:

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

我通过记录process.env进行了一些探索

当使用firebase函数在本地运行函数时:shell或firebase serve——仅函数有一组本地机器类型的节点变量

运行Firebase云函数中的已部署函数时。有一个新的节点环境变量在本地运行时未设置:

NODE_ENV: 'production'
因此,要使用它:

if (process.env.NODE_ENV === 'production') { 
    // running in production cloud environment 
} else { 
    // running locally (shell or serve) 
}

在本地运行程序或在谷歌电脑上远程运行程序之前,不必编辑程序,可以节省大量时间

object process.env在本地和在云中运行时都被断言。它有很大的不同,但我认为这里有一个属性是可靠的,易于理解和使用

这段独立代码说明了它的位置,并将变量端口设置为我在这两种情况下使用的常用数字

// Dan K The program wants to know, where am I ?
'use strict'

console.log( "program ID: " + "zincoNoDogs13" );

// Make a message and set a port number for other uses depending on whether the
// program wakes up on a local computer or in google cloud

const functions = require( 'firebase-functions' );
const express = require('express');
const app = express();
exports.api = functions.https.onRequest( app );

var port;
var imaThis = "local";
let lookie = process.env.HOME;
if( lookie == "/tmp" ) { imaThis = "cloud"; }

if( imaThis == "local" ) {console.log( "I am on a local computer" ); port = 3000; }
if( imaThis == "cloud" ) {console.log( "I am in the clouds man" ); port = 80; }

console.log( "Port number is going to be: " + port );
你得到一个或另一个,或者,无论如何,我得到:

程序ID:zincoNoDogs13 我是云中人 端口号将是:80

程序ID:zincoNoDogs13 我在本地计算机上
端口号将为:3000

Firebase现在在运行仿真器时设置
函数\u仿真器
环境变量:

if (process.env.FUNCTIONS_EMULATOR === 'true') {
    functions are on localhost
} else {
    functions are on Firebase
}

正确的(不包括Firebase添加)表明GCLOUD_项目是遗留项目,而GCP_项目是获取项目id的更正确的方法。目前,这是正确的方法:)
if (process.env.FUNCTIONS_EMULATOR === 'true') {
    functions are on localhost
} else {
    functions are on Firebase
}