Nodejs config which is subject to change - node.js

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.

Related

Visibility of files and global variables using pyTelegramBot

So i've been working on a chatbot on telegram using python (pyTelegramBot API), and started to look into saving users data in a database (.db) file.
My question can be devided into two questions:
While asking information from the user - the bot stores the input data from the user in a global dictionary that stores the user data. Now as far as I know - using a global variable is bad practice, but that was the best way I found to deal with storing the data across different functions.
I also read that using a User class will be better - but I fail to understand how a global object would be better. Maybe I'm missing something very obvious here but this is really stopping my project from advancing
About using .db file (SQLite3) - I guess that if someone would want to gain access to my db file, he would need to go on the file server and open the file itself. is there any way I can protect this file from being opened by anyone but me and my bot?
Thank you!

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.

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.

MEAN Stack- Where should I store images meant for particular users?

I am using the MEAN stack for my project. I read online that it is not advisable to store image in the database itself and hence I am not doing that.
For solving this issue, now I have set up a local server (Using express) and I am serving my static image files from there.
Now I am able to use that image by using the URLs, for example:
http://localhost:4200/images/a.jpg
I am planning to host this express app eventually by using some service like heroku.
In my main website, I am achieving authentication(Sign In and Sign Up) by using MongoDb and NodeJs.
I want the images to be shown according to the specific logged in user.
Should I store my images in folder named by username of that user, so that I can genarate the URL string accordingly and access the image by :
http://localhost:4200/user1/a.jpg
Is the flow of my application correct? Is this the way I should be accessing the images for particular users?
I read somewhere that there would be a security issue because anyone having the url of the image can access it. I am not much concerned with security now as this a small project which is not meant for many users. But any suggestions for a way in which there won't be such a security issue would be helpful.
I am new to this and any advice would be helpful.
Thanks in advance.
You could use firebase for this .
Its super easy
Over there you could just create a folder with any name ans save all the images.
In the database you could just save their firebase generated link which can easily be mapped using a user_id or something like it.

Triggers in Loopback

I have created a small application in loopback in which i was saving user email and password , password in the form of simple text.
Now we have got feature request , to save password in some encrypted format , say , Base64.
So what i want is whenever new user created save password in encrypted format , which is easy , which we can do it.
But issues comes we have created users from many different different code locations, so instead of updating code every place is it possible to add some form of trigger on password field , so whenever this field gets updated , will call a function , which will take password text , and update the same with encrypted format.
so we don't have to worry about writing code everyplace.
Let me know if the question is not clear.
I am looking for Loopback solution.
Thanks
Ever heard of loopback Operation hooks?
https://loopback.io/doc/en/lb2/Operation-hooks.html
use the before save hook or after save hook on a model to do what ever you want.
they have an example, you can try it out
If you use default user model, password is by default stored in encrypted form.
If you want custom trigger than you can do it using remote or operation hooks.
Operation Hook: https://loopback.io/doc/en/lb2/Operation-hooks.html
Remote Hook: https://loopback.io/doc/en/lb2/Remote-hooks.html
The password is stored in encrypted form. You can use operation hook.
Answering your question in the spirit and then in the letter .
Triggers are database level feature where as loop back is your server framework .Keeping that mind your best opportunity within loopback is to use Operation hooks like earlier answers suggested .
However ,if you insist on trigger then you need to use the drivers supported by the DB. In case of oracle there are nodejs-oracle and loopback-oracle connector .If you are using mongo ,which i assume you do , then you may use mongodb oplog which will give you life cycle method where you can hook your desired code .For performance reasons i wouldn't recommend such trigger solutions .
It is not clear from your question what different different code locations means .I assume it is diff parts of your application which might not be loopback .In which case ,putting an entry in a queue and handle it via a processor will be your best bet sothat you can solve the problem of multiple origin .

Resources