How to get the database connection string from the environment variables?
-
Is it possible to get the database connection string from the .env file?
Or it should be a part of jsreport.config.json only?
-
Here are the docs for configuration loading
https://jsreport.net/learn/configuration#configuration-sourcesSo the
.env
file isn't used. Use the config file or write a logic that loads.env
file inside the jsreport startup fileserver.js.
-
Could you please guide us on how to load the MongoDB connection string from .env file to jsreport startup file server.js?
-
You can use for example the following lib
https://github.com/motdotla/dotenvand simply add the documented code at the beginning of the jsreport
server.js
filerequire('dotenv').config()
You can do whatever you want in
server.js
file. It is interpreted with nodejs just like any other nodejs app. In other words, you can find solutions for such tasks just by searching nodejs forums, blogs...
-
We do not want to keep some sensitive details in the config file. We want to get these details from the .env file.
So, is the following configuration possible?
What if we want to keep only one common config file for all the environments and load environment-specific config from .env file in server.js?
-
This is our Dockerfile:
When we placed our MongoDB connection string in server.js:
It was throwing this error:
Could you please help us with this process?
- How do we use the MongoDB connection string in server.js?
- If we add certain configurations directly in server.js then do we need to remove the respective configurations from jsreport.config.json?
- If we want to specify config in server.js (using .env) then do we need to specify all of the configuration in server.js and discard jsreport.config.json?
- Do server.js and jsreport.config.json complement each other for configuration?
Please guide us with the correct steps.
-
How do we use the MongoDB connection string in server.js?
See the documentation
https://github.com/jsreport/jsreport/tree/master/packages/jsreport-mongodb-store
The mongodb connection string is passed like thisrequire('jsreport')({ "store": { "provider": "mongodb" }, "extensions": { "mongodb-store": { "uri": "mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test" } } })
If we add certain configurations directly in server.js then do we need to remove the respective configurations from jsreport.config.json?
See the documentation
https://jsreport.net/learn/configuration#configuration-sourcesThe options passed in the
server.js
has the highest priority