How to initialize Variables from Database and use them Globally in ExpressJs - node.js

I am building an API with NodeJs (expressJs).
I would be accessing 3rd party Services at several points using API Keys stored in my database(MySQL).
I was wondering if there was a way to initialize all the required API Keys from the database and save them as variables in a .Js file as soon as the server starts. Then require these files wherever the variables are needed.
I do not want to store the API KEYS plainly in any .js or .env file, but I want them to be found only in database, initialized at start of the program and then available everywhere.

Related

What is the best way to store node.js site settings?

I want to ask about best way of storing Node.JS config in project.
I tried store it in file like settings.json. But I always need to restart the server, or, in each place use
var settings = require('../settings.json');
Because I can update this file through admin panel and I want immediate changes.
If I use MongoDB, I must always call:
var settings = Settings.findOne({});
But can I store config, which I can edit and it will immediately change in all files?
If you need to share the file across components and you don't want to require everywhere, you can place it in the global namespace in node. i.e:
global.config = require('./yourFile.json');
global is accessible from all your scripts so you don't need to require the config everywhere and if you overwrite values in the file you can create a service that writes to the file using fs.writeFileSync (to persist your changes) and updates the config in the global namespace.

Environment Variables in Angular2

So I have an angular 2/4 app, with a node server.js to serve it. I want to access an environment variable (backend endpoint is localhost for dev and another endpoint for prod). If it matters, this app (as well as the backend app) are meant to be deployed to Heroku.
I have tried finding a solution to this, but everything I found seems to lead to using multiple environment.ts files (1 for each configuration), and then swapping between them based on the ng build flag --env. But I am apprehensive about this solution as it seems to mix configuration and source code. I also would like to avoid (if possible) being required to re-build/re-deploy whenever an endpoint/config changes.
In Spring Boot, I'm used to using config like ${PORT:8080} in the properties files. Though I've also created multiple .properties files in the past so I understand that sometimes it's just simpler/cleaner to do so.
In node, I'm used to doing env variables like process.env.PORT || 8080. Since my angular app is served by a node server.js, I would think I should be able to do something similar. But when I try using process.env in my environments/environment.ts file, I get the error Cannot find name 'process'.
So my question is essentially, if I am required to create multiple environment property files or is there an alternative.
Thanks
You basically have two choices, regardless of whether your config is stored in environment variables or in a config file on the server. Your clientside code needs it, so you either need for serve it as part of your assets bundle, or you have to fetch it as a separate request. That separate request could be an Ajax call or just a js file that gets served separately from the rest of the bundle.
I think ideally, the only difference between the production app and the nonproduction app on the client should be the data, the bundle density (in dev you don't want your stuff minified for example) and what url it's served from. Everything else should be discoverable from the source url, imo

Sharing memory-only NeDB instance in multiple Electron BrowserWindows

We are developing an app using Electron and Vue.js. In our app we are using NeDB for temporarily storing JSON documents after having received (and decrypted) them from a Firebase Database. A requirement of the app is, that the decrypted data stays in memory and is not saved to disk during the user session. Therefore we use NeDB with the inMemoryOnly flag.
Our goal is to show reports for contents of the database in a different BrowserWindow to print / save them as PDF.
We tried to initialize the database using a global variable, but unfortunately the database content was empty. Is there another possibility to access the database from within a different BrowserWindow?

Insert an Azure Slot Setting into a Javascript File

Is there a way, possibly in deploy.cmd, to alter the contents of a file? Either ALL of the contents, or Search and Replace, or Grep, or anything?
I need a way to alter an environment variable in a javascript file based on a Slot Setting in Azure.
Or, put another way: I want to take a Slot Setting in an Azure slot, and insert it into a javascript file.
No sure why you want to inject those values in the runtime instead of during build and deployment. But here are a few possible solutions:
Azure App Service slot settings are exposed as environment variables, your javascript could make an ajax call back to the server to an API you develop that can return the values of those environment variables.
Inject the value from your app's backend before serving the JS file to the client on the fly (and maybe cache it), in this case, you have to intercept the request for that file and serve your modified version.
Add a json file to your website, let's call it config.json, and change its content for each slot based on your requirements. Then your JavaScript app can read that file by issuing an AJAX GET to /config.json, which will represent the slot config.

Where do I store sensitive data in a Node.js app on GitHub deployed to Heroku?

Right now my app is still in dev mode and my sensitive data like database username/password, keys, secrets etc. are stored in a config.js file.
This file is part of the Github repository of this app like the rest of its files. It's a private repository (but not a local one) where only I have access right now.
Whenever I update a file locally and push it to Github, Github then pushes the updates to Heroku which updates the live Node.js app.
I don't want my config.js file to be part of this repository anymore. i.e. I don't want it to be accessible anywhere other than from within Heroku.
In other words I want to upload that file directly to the Heroku directory where my app is stored.
Whenever, I want to update any of my sensitive data I will have to go and find that file in the Heroku directory and update/edit it from there.
Isn't that the best/safest way to store that data? Is there another way?
I know about environment variables but I don't want to have to set them everytime I start my app, that's why I want to retrieve my sensitive data from a stored file.

Resources