A
A
a1en_yeah2016-10-03 15:27:49
Node.js
a1en_yeah, 2016-10-03 15:27:49

How to connect a configuration file to the node.js server, with the ability to modify it?

There is a server in which the config.json configuration file is connected via require('../config/config.json'), and if I understand correctly, it is loaded 1 time at startup. But if in the admin panel on the client I added the ability to change the config, then how to apply these changes immediately, without restarting the server?
I guess using (fs.readFileSync(__dirname + '/../config/config.json')) in every function is not the best choice.
I suppose that you can create a function for loading a file, and run it at server startup and when changing the config, but I suspect that this can create problems at the start (you need to run it first of all that uses it) since I have no experience, I'd better advise.
Thank you colleagues =)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mark Doe, 2016-10-03
@a1en_yeah

You can use for example HotConfig , a small module that provides "hot" loading of the config in real time. Just what you really need

M
Matvey Safronov, 2016-10-04
@impeee

Either nodemon, which will still reload the application, or reload the file each time, or create some kind of timer that would track the state of the file, and in case of a change, load the file into the node's memory. There is another perverted way - load the config into environment variables, and pull it out using process.env )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question