使用不带Node Server的AppSettings创建React App [英] Create React App using AppSettings without Node Server

查看:111
本文介绍了使用不带Node Server的AppSettings创建React App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用create react app将Web应用程序部署到Azure App Services。我想在我的React UI应用程序中使用App Settings Environment变量。 (process.env.NODE_ENV,process.env.RANDOM_VARIABLES)等。每当我尝试这样做
并将我的构建文件夹上传到Azure应用服务时,我的所有变量都会以未定义的形式返回。有没有办法让我在没有为React应用程序设置Node Server的情况下这样做?是否有特定的设置或类似的
我需要将应用程序部署到Azure App Services并能够访问React客户端代码上的process.env以访问内部配置的AppSettings Azure?

I have a web app deployed to Azure App Services using create react app. I am wanting to use the App Settings Environment variables inside of my React UI application. ( process.env.NODE_ENV, process.env.RANDOM_VARIABLES ) etc. Whenever I try and do this and upload my build folder to the Azure App Services, all of my variables are coming back as undefined. Is there a way for me to go about doing this WITHOUT having a Node Server setup for the React application? Is there a specific setup or anything like that I will need in order to deploy the app to Azure App Services and be able to access the process.env on the React client side code to get access to the AppSettings configured inside of Azure?

推荐答案

你好mwarnsley,

Hi mwarnsley,

是的,我相信这是可能的。 你现在好吗?上传你的应用程序?

Yes, I believe this is possible. How are you currently uploading your app?

在门户网站中,应用程序设置部分包含您的网络应用程序在启动时加载的名称/值对。对于
.NET apps ,这些设置会在运行时注入.NET配置App Settings,覆盖现有设置。

In the portal, the App settings Section contains name/value pairs that your web app will load on start up. For .NET apps, these settings are injected into your .NET configuration App Settings at runtime, overriding existing settings.

PHP,Python,Java和Node apps 可以在运行时将这些设置作为环境变量进行访问。对于每个应用程序设置,都会创建两个环境变量;一个具有app设置条目指定的名称,另一个具有前缀
的APPSETTING_。两者都包含相同的值。  NODE_ENV = production设置环境变量,告知Node.js在生产环境中运行。 node server.js在您的存储库根目录中使用server.js启动Node.js服务器。这就是您在Azure中加载Node.js
应用程序的方式。

PHP, Python, Java and Node apps can access these settings as environment variables at runtime. For each app setting, two environment variables are created; one with the name specified by the app setting entry, and another with a prefix of APPSETTING_. Both contain the same value. NODE_ENV=production sets the environment variable that tells Node.js to run in the production environment. node server.js starts the Node.js server with server.js in your repository root. This is how your Node.js application is loaded in Azure.

您可以使用 process.env.ENV_VARIABLE
读取
Node中的环境变量。您可以尝试强制
environmental变量
到脚本中的package.json代码中。有关详细信息,请参阅
在Node.js中读取环境变量
。此外,此文档将向您展示如何在Azure App Service上部署您的节点应用程序。

You could use process.env.ENV_VARIABLE to read environment variables in Node. You can try to force environmental variables into the code from package.json in the scripts. See Read environment variables in Node.js for details. Also, this documentation shows you how to deploy your node app on Azure App Service.

希望有所帮助。如果您有其他问题,请告诉我们。

Hope that helps. Please let us know if you have additional question.


这篇关于使用不带Node Server的AppSettings创建React App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆