Express: the best way to share the JS configuration file between client and server

I have a config.js file that has all the settings for my application. Some of these parameters should be used in conjunction with external scripts, as well as with internal interfaces.

I have some confidential variables that I do not want to show in the interface. Do any NodeJS / ExpressJS wizz kids have any good solutions?

Do you keep your shared configuration data in the location /public and confidential data on the server side separate from the open /public folder?

Thanks guys,

+4
source share
4 answers

Using express-expose to share (or "disclose") data from server to client works very well. As Rainos said, I would save confidential variables as environment variables, and then you can safely expose all your settings.

Or you can do something like creating a β€œcommon” object in your settings. Like this:

 app.set('shared', { setting1: mydata , setting2: new OAuth(...) , setting3: { ... } }); 

Which allows you to access settings like app.setting.shared.setting1 on the server, and then you can app.expose(app.setting.shared) so that your client side JS can access the same object.

+7
source

You can use express-expose to share your variables with the interface, or you can use browserify and share your settings in the module.

+2
source

I have some confidential variables that I do not want to show in the interface.

I usually have confidential variables defined in environment variables. It also solves the issue of committing / hard coding these variables in version control.

+1
source

There is also express-state from Yahoo, it allows you

share client configuration and Express application state data with JavaScript

This is well documented and readme contains many useful examples.

0
source

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


All Articles