【问题标题】:change the configuration of an application based on environment setting: Production vs Development根据环境设置更改应用程序的配置:生产与开发
【发布时间】:2019-03-17 01:12:15
【问题描述】:

我在 Google App Engine 上使用 React 和 Node(灵活的环境)。

我希望能够根据环境设置更改我的应用程序的配置:生产与开发。

在开发中,我有以下设置: 我使用 localhost:5050 在浏览器中运行我的反应应用程序并在我的 localhost:8080 上运行节点应用程序 在 package.json 我使用代理: “代理”:“http://localhost:8080” API 请求使用本地 URL:“localhost:8080/something”

在生产中,我有以下设置: 无需代理; API URL 是:“https://www.example.com/something

如何确保当我在 localhost 中启动应用程序时,我的 API 请求将使用本地域并使用代理? 有没有办法通过 package.json 或其他选项来配置它?

【问题讨论】:

    标签: node.js reactjs google-app-engine


    【解决方案1】:

    您需要使用 .env 文件来指定您的 API URL。

    如果您使用的是 CRA,请参阅此问题的答案:How to set build .env variables when running create-react-app build script?

    如果您使用的是 webpack,请参阅本教程:Using environment variables in React

    附: package.json 中的代理功能并不像 answer 中提到的那样用于生产,因为它只是一个开发功能。

    【讨论】:

      猜你喜欢
      • 2011-08-06
      • 2017-05-08
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 2015-12-07
      • 2021-09-10
      • 1970-01-01
      相关资源
      最近更新 更多