简体   繁体   English

基于配置文件的 js/Nodejs/ES6 配置

[英]Profile based configuration for js/Nodejs/ES6

I've an application with ES6/js/react UI and Spring boot server side.我有一个带有 ES6/js/react UI 和 Spring Boot 服务器端的应用程序。 In my config.js I have host server URL and other properties that changes based on Env.在我的 config.js 中,我有主机服务器 URL 和其他基于 Env 更改的属性。 I can use spring active profile to pick up diff set of properties.我可以使用 spring 活动配置文件来获取不同的属性集。 How do I do similar thing on the js part?我如何在 js 部分做类似的事情? The application is deployed as spring boot application with embedded Tomcat.该应用程序部署为带有嵌入式 Tomcat 的 Spring Boot 应用程序。

Another way to put the question is How can I do Spring profile equivalent in NodeJs/JavaScript world?提出问题的另一种方法是如何在 NodeJs/JavaScript 世界中做等效的 Spring 配置文件?

My suggestion is to create a server using nodejs to host your static files and act as a proxy server to your backend.我的建议是使用 nodejs 创建一个服务器来托管您的静态文件并充当您后端的代理服务器。

Lately I've used this this seed for my react-projects.最近我用这个这个种子我反应过来的项目。 I like it a lot, it uses webpack for building the frontend and also provideds you with a handy proxy server that you can use during development (which also gets rid of any CORS issues).我非常喜欢它,它使用 webpack 来构建前端,还为您提供了一个方便的代理服务器,您可以在开发过程中使用它(这也消除了任何 CORS 问题)。

Settings for proxy server: environments.js代理服务器的设置: environments.js

I had exactly the need as you: how to use the same idea behind spring profile active environment variable.我和你一样需要:如何在 spring 配置文件活动环境变量背后使用相同的想法。 I used process.env:我使用了 process.env:

const profile = process.env.NODEJS_PROFILES_ACTIVE;

I tested with Windows and Docker as an environment variable.我使用 Windows 和 Docker 作为环境变量进行了测试。

I have implemented the following and it seems to work .我已经实施了以下,它似乎工作。

Set up files like config-dev.js, config-QA.js, config-prod.js .设置文件,如 config-dev.js、config-QA.js、config-prod.js。 These hold the configuration specific to each environment这些包含特定于每个环境的配置

Ex config-dev.js前 config-dev.js

const config_params = {dburl:"mongodb://localhost/mydb", port:5000};
module.exports = config_params

Set up file - config.js which has the below logic设置文件 - config.js 具有以下逻辑

let loadmodule = "./config-" + process.env.profile + ".js";
const setting = require(loadmodule);
module.exports = setting;

In the index file or server.js, import config.js在索引文件或 server.js 中,导入 config.js

const config = require('./config.js');

Start the application as 'profile=dev node server.js''profile=dev node server.js'启动应用程序

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

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