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

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.

Related

How to initialize Variables from Database and use them Globally in ExpressJs

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.

Nodejs config which is subject to change

I was wondering what the best way is to save config values in nodejs/express which are subject to change. I'm not talking about database credentials that you store inside your environment variables. But say a game where the config values are changeable within the admin panel. A config for example could be how often in ms a player receives a random reward. I could store this directly into the code (but I'd have to alter my code everytime I would like to change this and this cant be changed from within an admin panel), have some different file that has an object with these configs or save it into a mysql database (I would need to query the db everytime I need a config though).
I'm not really sure what the best way is to do this. Basically I want a way to store flexible configs within my nodejs application that are subject to change.
Thanks in advance.

Where to save data in express.js app other than database?

I am showing a link on a page and I want to change that link, by using a form.
where should I save that link and retrieve to render the page other that a database?
*I think involving database for such a small task is not performance efficient.
what I can do is save it in a global variable as a string, so that I can access it and change it.
But is it a good practice to use global variable for such task in production?
OK, now that we know that this is a change that you want to affect all users, then a global or preferably a module-level variable or a property on a module-shared object would be fine.
If you want this change to be persistent and survive a server restart, then you would need to write the change to disk somewhere and have code that reads that setting back in from disk when your server restarts. A simple way to do that might be to read/write it to a file in the JSON format. That gives you simple extensibility to also include other settings in that file.
Your admin form can then just update this variable and trigger a save to disk of all the current settings.
I believe which approach you want to implement really depends on the scenario e.g. how frequently will the link be changed, how frequently will it be read etc. A couple of suggestions:
If different user wants to see and update the same link without affecting others, you can use on client side stored cookies. Then you won't need a db but each user will manage their own link that no one else can access.
You could use a file e.g. json or simple text file and use built in fs to read and write into that file. However in that case you would want to use sync operations to avoid concurrency issues e.g. const contents = fs.readFileSync('storage.txt', 'utf8');
Of course you could store data in a string too, however if server was to go down, the link would not persist.

Export Nedb contents

I have an electron app that acts as a tool to speed up game development. When a user opens the app they can begin importing assets and setting custom properties. I store these properties in nedb. The way I have my application set up, every page change the properties from nedb are fetched and are immediately reflected on the page. I need to save these changes in a file the app can load and it would be intuitive to use nedb since it loads database info from a file optionally out of the box. However in the instance a user opens my app and begins working without a file, I have no way to store these changes for later use. I have found no docs for exporting the database on the fly.

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.

Resources