Node-Red mongodb3 connect DB using URL from environment variable - node.js

I'm running Node-Red embedded in Express application. Also using 'dotenv' to load environment variables.
For storage using MongoDB with 'node-red-contrib-mongodb3'.
Everything works as expected. But, I have different environments and different MongoDB for each environments.
I want to connect to MongoDB from configuration (.env file or environment file).
Something like, in MongoDB config node URL input box golbal.get('env').MONGODB_DEV_URL or msg.MONGODB_URL
Tried looking for an option in the documentation of 'mongodb3' and google, still no luck. Any help or direction will be appreciated.

From the Node-RED docs
Any node property can be set with an environment variable by setting
its value to a string of the form ${ENV_VAR}. When the runtime loads
the flows, it will substitute the value of that environment variable
before passing it to the node.
This only works if it replaces the entire property - it cannot be used
to substitute just part of the value. For example, it is not possible
to use CLIENT-${HOST}.

Related

How To Fix: Invalid "database" config. "local" is not defined inside "connections". Make sure to set it inside the "config/database" file Adonis Js

I got an exception from Redis it says:
Invalid "database" config. "local" is not defined inside "connections". Make sure to set it inside the "config/database" file.
To Implement Throttle. I download Two Packages:
1) Adonis-Request-Throttler
yarn add adonis-request-throttler
2) Redis
yarn add #adonisjs/redis
And ConfigureThen as the instruction shown in Adonis Js Official Documentation.
node ace configure #adonisjs/packagename
I also pest Validate environment variables in env.ts file. But When I try to run my app I got an exception :
Please Help me To Fix This. I have tried everything but it's not working and my project is is running.
That problem is because your config/database file is not properly configured.
Set the variables as recommended in the documentation depending on which database driver you are using.

Unable to properly define MongoDB connection string as an Travis CI environmental variable

I'm working on my own project using Node.js and MongoDB as a database (using Mongoose).
When I developed it locally, I defined my environmental variables in a .env file and used 'env-cmd'.
Now I'm trying to create a testing workflow using Travis-CI without using a .env file so I defined my environmental variables on the repository setting but I keep getting an error that the connection string is invalid when trying to build:
It is worth mentioning that:
I escaped every special character when defining the env variable of the connection string.
I tried passing both process.env.MONGODB_URL with and without the ES6 backticks format to the mongoose.connect() method.
The connection string is valid (I tried running my test suites locally with the .env file and it worked)
The connection string is to the MongoDB Atlas database, not a local one.
I'm running the tests from as a docker container.
I can't figure out what is the problem, and if any additional information is needed to tackle this problem I will happily provide.
Thanks in advance!

Setting up environment variables in node, specifically, GOOGLE_APPLICATION_CREDENTIALS

I have a node application and I'm trying to use the google language api. I want to set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the json file in the same directory (sibling to package.json and app.js).
I had tried process.env.GOOGLE_APPLICATION_CREDENTIALS = "./key.json"; in my app.js file (using express), but it isn't working. I have also tried putting "GOOGLE_APPLICATION_CREDENTIALS":"./key.json" in my package.json and that didn't work as well. It DOES work when I run in the terminal export GOOGLE_APPLICATION_CREDENTIALS="./key".
Here is the error message:
ERROR: Error: Unexpected error while acquiring application default credentials: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information.
Any tips are appreciated, thanks!
After reading and reading about this issue on the internet, the only way to resolve this issue for me was to declare the environment variable for the node execution:
GOOGLE_APPLICATION_CREDENTIALS="./key.json" node index.js
Because I was able to print the token from my server console, but when I was running the node application, the library was unable to retrieve the system environment value, but setting the variable for the execution, it was able to retrieve the value.
It could be that the environment variable in your OS was not accurately set. For example in Linux you usually have to set GOOGLE_APPLICATION_CREDENTIALS in the terminal where you executed your app.
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
Another option you have is passing the json path by code. It is documented this process using Node.js with the Cloud Storage.
Just to update this thread.
Relative paths with GOOGLE_APPLICATION_CREDENTIALS now works fine with dotenv :)
Just store your service-account credentials as a file in your project and reference it relative to your env-file as a path. Then it should work fine :)
I encountered the same issue. I was able to get it working by doing the following:
export GOOGLE_APPLICATION_CREDENTIALS="//Users/username/projects/projectname/the.json"
The issue is covered mostly in the docs here.
the command varies slightly depending on what your OS is:
The GOOGLE_APPLICATION_DEFAULT environment variable you are setting is accessed by the google client libraries - it wont work with a relative path, you'll need to set the absolute path.

Setting long environmental variables in nodejs

On a Node server that will be deployed to heroku, I have to load a long private key from the environmental variables. When I declare my environmental variable in a .env file in the traditional method (key=value, with no linebreaks if value is long), run it locally (through 'heroku local') and console.log the loaded environmental variable value, the console shows it has been split into several lines (but not around the '\n' character).
Loading the string from environmental variable:
Loading the string when it's hardcoded into the server and console logging shows:
I switched to foreman to load environmental variables and used the JSON syntax to get it working. My question is, on the traditional method, why does the console show the string is split up?
Loading the long value as this value is required for Firebase 3 SDK initialisation, as Heroku does not allow you to load the private key file itself.
For local development, the solution is to start the server using Node-Foreman and load the environmental variables from the .env file through the node-foreman utility. Use the JSON format for the environmental variables (not the key=value format). The JSON usage is described in the documentation.

Change configuration in runtime by changing environment variables using the module node-config

I'm trying to use the node-config module to change some parameters of my configuration (basically logging level) during runtime.
In the official documentation says:
Environment variables can be used to override file configurations. Any environment variable that starts with $CONFIG_ is set into the CONFIG object.
I've checked that this is true when the server starts but it does not seem to work once it's up. (The handler of the watch function is never called when an environment variable is changed unlike a change in the runtime.json file or directly changing a config variable).
I'm currently watching the whole CONFIG object like this:
var CONFIG = require('config');
CONFIG.watch( CONFIG , null , function(object, propertyName, priorValue, newValue){
console.log("Configuration change detected");
});
Does anyone know if this is possible?
The environment is available during startup of a process.
If the process is running, you won't be able to change the environment anymore, the process is in.
The only option is to restart the process or use other mechanisms to communicate with it.
Say for example having a rest or tcp listener inside, where you can transfer your variable inside.
Best regards
Robert
As you must knowing, React is a single page application which is eventually when it is complied is a static page app that means all the files of the react application is complied into vanilla JS and CSS file bundle in a Tarball. Now that Tarball is eventually deployed on a web server. It could be Apache web server, nginx web server or anything which you are using it but an important point is the static app is running in someone else browser and someone access to website CSS and JS are downloaded in a browser and it is running in the browser runtime environment so technically you cannot have a runtime environment variable for someone else browser but may be there would be a way to access them during runtime.
SOLUTION
I have achieved this goal with the package called runtime-cra.
follow the steps on this official documentation: https://blog.risingstack.com/create-react-app-runtime-env-cra/

Resources