简体   繁体   English

如何将配置文件存储在 React 的构建文件夹中?

[英]How to store a config file in build folder of React?

I want to store my app on different servers.我想将我的应用程序存储在不同的服务器上。 Each server has it's own config like contact address, email-address, route path names and so on.每个服务器都有自己的配置,如联系地址、电子邮件地址、路由路径名称等。

Currently I have different config.js files inside my src folder and when there is an update of the app, I need to rebuild the app for each server and replace the config.js every time.目前我的 src 文件夹中有不同的 config.js 文件,当应用程序更新时,我需要为每个服务器重建应用程序并每次都替换 config.js。

Is it possible to store the config.js in the build folder so that the app coming from yarn build is always the same on each server but the config.js stored on the server is different?是否可以将 config.js 存储在 build 文件夹中,以便来自yarn build的应用程序在每台服务器上始终相同,但存储在服务器上的 config.js 不同?

That would be much easier because currently building the app is a pain.这会容易得多,因为目前构建应用程序很痛苦。

In your Client Side Code Create new File called Config.js > Place your configurations there Example:在您的客户端代码中创建名为Config.js的新文件 > 将您的配置放在那里示例:

module.exports = {
PORT: 80,
APPID: "xxxxx",
APPCERT: "xxxx",

}

Import your file wherever you would like to suppose in my case server.js I imported it as follows在我的案例 server.js 中将您的文件导入到任何您想假设的地方 我按如下方式导入它

var env = require('../../Config');

then use your configs as you defined them然后使用您定义的配置

var appCertificate = env.APPCERT;

Note: in order to keep your config file dynamic you can use Environment Variables or make different config.js files for your different environments.注意:为了使您的配置文件保持动态,您可以使用环境变量或为不同的环境制作不同的 config.js 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM