简体   繁体   English

在部署 React JS 应用程序期间如何使用不同的配置文件?

[英]how to use different configuration files during deployment of react js app?

I have 3 environments and I am using Sharepoint as backend.我有 3 个环境,我使用 Sharepoint 作为后端。 Each environment has got fields which are called differently.每个环境都有不同名称的字段。 The problem is that I can't change the internal names of these fields.问题是我无法更改这些字段的内部名称。

I am using REST API to call SharePoint endpoints as follows:我正在使用 REST API 调用 SharePoint 端点,如下所示:

/sites/someSite/_api/web/lists/getbytitle('list1')/items?$select=field1,field2[etc]

In my react app , I would like to achieve the following:在我的react app中,我想实现以下目标:

  1. store this relative url /sites/someSite somewhere at the root of the application and access it everywhere in the application将这个相对 url /sites/someSite存储在应用程序根目录的某个位置,并在应用程序的任何地方访问它
  2. each environment has some fields which differ in name, for example:每个环境都有一些名称不同的字段,例如:
  • dev: field1开发者:field1
  • acc: field1 is called col1 acc: field1 被称为 col1
  • prod: field1 is called fieldCol1 etc产品:field1 被称为 fieldCol1 等

these fields are used in my rest api queries.这些字段用于我的 rest api 查询。

How can I configure my application so that during deployment, a configration file can be
 used or some other means to assign the correct fields to each REST API call?

I can use sort of if else branch, but that does not seem realistic.我可以使用某种 if else 分支,但这似乎不现实。

Dev:开发:

/sites/someSite/_api/web/lists/getbytitle('list1')/items?$select=field1,field2,field3,field4, etc

Acc:帐户:

/sites/someSite/_api/web/lists/getbytitle('list1')/items?$select=col1,field2,col3,field4, etc

Prod:产品:

/sites/someSite/_api/web/lists/getbytitle('list1')/items?$select=fieldCol1,fieldCol2,field3,field4, etc

For this you can maintsin 3 seperate configuration/env files for your application (dev.env, ...).为此,您可以为您的应用程序维护 3 个单独的配置/env 文件(dev.env,...)。 First instlall using npm dotenv and env-cmd dependencies.首先使用 npm dotenvenv-cmd依赖项进行安装。 Add following script to the package json file.将以下脚本添加到 package json 文件中。

    scripts": {
        "start:dev": "env-cmd -f dev.env npm start" 
        "start:prod": "env-cmd -f prod.env npm start"
...
}

If you want to start react app using dev environment.如果您想使用开发环境启动 React 应用程序。 You can use您可以使用

npm run start:prod

Then mentioned url like below.然后像下面提到的 url。

"/sites/someSite/_api/web/lists/getbytitle('list1')/items?$select="+ SELECT

You can use require('dotenv').config() to read the environments.您可以使用require('dotenv').config()来读取环境。

dev.env file like below. dev.env 文件如下。

SELECT = col1,col2...

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

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