How to configure a node application (ember application suite) on a hero who reads ENV variables and makes the values ​​available to the application?

Ok, I'm new to node, and actually just using node server to serve static js, but I can't find any information about this anywhere.

I run the application the ember application suite that is created for node server.js to be deployed, and heroku runs it with node server.js .

It uses grunt to create, test, etc.

I would like to know how I can specify configuration variables (i.e. authentication tokens) that can be overridden by heroku configuration variables.

The closest I could get is a user task that reads environment variables and writes a json file that is embedded in the site (and assigned to the global var). This works locally, but does not take into account the configuration of heroku.

I even wrote a deployment script that receives the heroku configurations, exports them as environment variables locally, and makes an assembly - which works, but the configurations are only updated in the deploy application. Therefore, if I do heroku config:add CONFIG_TEST=test_value , my application does not see this value for CONFIG_TEST until the next time I deploy the application.

I would like my application to immediately enable this configuration value in the JS browser.

How to do this with node, how is my application configured?

+4
source share
2 answers

I'm not sure I understand what is wrong by simply accepting configuration variables at runtime from the environment. Use process.env.KEY in your code and paste this result into any template you have and serve it as the result.

When Heroku configuration parameters change, your process restarts, so it takes on new values.

Is the problem the fact that you are serving static files? If so, can you just modify it to use the template engine to perform some operations on them before serving?

+1
source

OK, here is the solution for ember-app-kit using grunt-sed.

In EMBER_APP_KIT_PROJECT / tasks / options / sed.js

Add something like

 module.exports = { version: { path: "./dist/", pattern: '{{env.API_BASE_PATH}}', replacement: function(){ return process.env.API_BASE_PATH; }, recursive: true } }; 

then in your code just type

 "{{env.API_BASE_PATH}}" 

Now when you run

 $ grunt sed 

it will replace "{{env.API_BASE_PATH}}" with what is in the environment variable.

0
source

Source: https://habr.com/ru/post/1496925/


All Articles