API Urls配置在React中

huangapple go评论87阅读模式
英文:

API Urls config in React

问题

我正在为我的公司开发一个产品。这个产品不会是SAAS,将部署在每个客户的服务器上。

鉴于此,我搜索了如何在React中存储API URI的方法,但我只找到了CRA提供的环境变量(也称为.env文件)的用法。

这并不符合我的公司要求,即只需“构建一次,随处部署”,使用一个配置键值文件(例如像'APIURI': 'http://foo.bar'这样的东西),在运行时读取,而不是每次部署时都必须构建应用程序并更改这些环境变量。

我尝试过读取外部JSON文件,但CRA会阻止在src文件夹之外进行导入。我还尝试了这种方法:https://stackoverflow.com/a/52184642/12570096,但最后它也注入了值,所以每当我更改配置文件时,它不会更新,所以...
我想知道是否有人知道如何实现这种情况。

谢谢大家!

英文:

I am developing a product for my company. This product will not be SAAS and will be deployed at each client's server.

Provided that, I googled how to store api uris in React, but I just found about the using of enviroment variables provided by CRA (AKA .env file).

This does not fulfill my company requirements, that is just "build once, deploy anywhere", with a config key-value file (for instance something like 'APIURI': 'http://foo.bar') read at runtime instead of building the app each time I must deploy it, changing these enviroment variables.

I tried to read an external JSON file, but CRA blocks any import outside the src folder.
I also tried this approach: https://stackoverflow.com/a/52184642/12570096 but in the end, it injectes the values too, so whenever I change the config file, it does not updates, so...
I wonder if anyone knows any approach to achieve this scenario.

Thanks all!

答案1

得分: 1

你应该获取配置而不是导入它。在你的应用程序中,而不是使用以下代码:

import config from '../config.js';

使用

const config = fetch('/config.json')
// .then(...)

你的应用程序随附默认的_config.json_(与你的_bundle.js_一起)。然后每家公司都可以自行编辑它。

英文:

You should fetch the config instead of importing it. In your app instead of

import config from '../config.js';

Use

const config = fetch('/config.json')
// .then(...)

You ship your app with default config.json (next to your bundle.js). Then every company can edit it on its own.

答案2

得分: 1

对于对这个主题感兴趣的人,使用Redux,您可以在应用程序的开头获取config.json(如Petr建议的)。

之后,任何动作创建器的第一行代码都必须获取当前状态(我们之前加载了配置的地方),在那里我们可以获取配置:

export const fetchUsers = () => (dispatch, getState) => {
    const config = getState().config.config;
    const getUsers = `${config.API.BASE}/${config.API.USERS}`;
}
所以getUsers包含了配置存储中的URL
英文:

For those interested in this topic, using Redux you can fetch the config.json (as Petr suggested) at the beggining of the app.

After that, the first code lines from any action creator, have to get the current state (where we previously loaded the config) and there we can obtain the config:

export const fetchUsers = () => (dispatch, getState) => {
    const config = getState().config.config;
    const getUsers = `${config.API.BASE}/${config.API.USERS}`;
}

so getUsers contains the urls from the config store

答案3

得分: 0

由于您只使用客户端应用程序

**问题**

首先,必须明确在浏览器环境中没有环境变量这一事实。无论我们现在使用哪种解决方案,都只是一种虚假的抽象。

但是,您可能会问,那么.env文件和以REACT_APP为前缀的环境变量呢?它们直接来自文档,是吗?即使在源代码中,这些变量也像我们在Node.js中使用环境变量一样被用作process.env。

实际上,在浏览器环境中不存在process对象,它是特定于Node的。默认情况下,CRA不进行服务器端渲染。它无法在内容提供期间注入环境变量(就像Next.js那样)。在转译过程中,Webpack进程会将所有process.env的出现替换为给定的字符串值。这意味着它只能在构建时配置。

因此,有一些解决方法,甚至我也在这里搜索了答案 [这里][1]

由于没有找到确切的解决方案,我找到了一个参考 [解决方法][2]


  [1]: https://stackoverflow.com/questions/59559592/passing-env-variables-on-the-run-time-of-docker-for-create-react-app-of-react-ap
  [2]: https://www.freecodecamp.org/news/how-to-implement-runtime-environment-variables-with-create-react-app-docker-and-nginx-7f9d42a91d70/
英文:

Since you are using only client application

Problem

First of all, it must be clear that there is no such thing as environment variables inside the browser environment. Whichever solution we use nowadays is nothing but a fake abstraction.

But, then you might ask, what about .env files and REACT_APP prefixed environment variables which come straight from documentation? Even inside the source code, these are used as process.env just like we use environment variables inside Node.js.

In reality, the object process does not exist inside the browser environment, it’s Node-specific. CRA by default doesn’t do server-side rendering. It can’t inject environment variables during content serving (like Next.js does). During transpiling, Webpack process replaces all occurrences of process.env with a string value that was given. This means it can only be configured during build time.

So there are some work arounds, even i was also searched for a answer on this here

Since there was no exact solution i have found a reference workaround

huangapple
  • 本文由 发表于 2020年1月3日 20:47:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定