最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React JS - Reading environment configurations from external property file - Stack Overflow

programmeradmin6浏览0评论

Problem : I am new to React JS, and looking for an option to read environment configs from an external property file. This problem is more specific for one of my clients, who is looking to have an option to change the environment files dynamically. E.g. change the hostname/port dynamically whenever there is a change. The build process is not owned by my client. I create a minified final package, which my client deploys it on tomcat/web server.

Tried Solution : With some read-outs, I have configured .env files for different environments and able to successfully read configs from these files. However, these are more of a build process environment files. And, I am trying to find a way to read the configs from an external source after my package is created.

Possible solutions : Here is one possible approach I can think of -

Read external property file using libraries like "properties-reader". I will provide the property file as part of my release bundle (i.e. build folder). My client can change this property file whenever required.

Please suggest if this is the correct approach or is there a better solution to this problem?

Problem : I am new to React JS, and looking for an option to read environment configs from an external property file. This problem is more specific for one of my clients, who is looking to have an option to change the environment files dynamically. E.g. change the hostname/port dynamically whenever there is a change. The build process is not owned by my client. I create a minified final package, which my client deploys it on tomcat/web server.

Tried Solution : With some read-outs, I have configured .env files for different environments and able to successfully read configs from these files. However, these are more of a build process environment files. And, I am trying to find a way to read the configs from an external source after my package is created.

Possible solutions : Here is one possible approach I can think of -

Read external property file using libraries like "properties-reader". I will provide the property file as part of my release bundle (i.e. build folder). My client can change this property file whenever required.

Please suggest if this is the correct approach or is there a better solution to this problem?

Share Improve this question edited Nov 11, 2019 at 6:24 Manu asked Nov 11, 2019 at 5:49 ManuManu 1732 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

A Solution which worked for me !!

1) Create a "config.js" file inside public folder of react project. Sample Content of the "config.js" file -

window.env = {
  API_DOMAIN_ADDR: "http://localhost:8080"
};

2) Refer "config.js" file inside index.html. Code for index.html will be -

<body>
    <div id="root"></div>
    <script src="%PUBLIC_URL%/config.js"></script>
  </body>

3) Now, content of the config.js file will be accessible to react code. Sample code to retrieve the value of config.js variables -

window.env.API_DOMAIN_ADDR

Add this code wherever variable value needs to be accessed. I added this in my service class which is making ajax call.

I would suggest using something like Firebase Realtime DB. I had a similar requirement for pointing the App builds to production or development server APIs for my pany. For this, we use to load a Firebase Config and from there the UI used to pick up the host server endpoint.

Advantages:

  • This saves you from deploying your build folder every time.
  • This is realtime and less prone to errors.
  • FirebaseDB is free for small stuff like this.

The second option is to create two environment files which I see you have already done.

发布评论

评论列表(0)

  1. 暂无评论