0

Basically the JS file is a config file, where if an update is done to the file, changes will reflect real time in the NodeJS Server without restarting.

I've tried using fs.watch() then reading the file via fs.createReadStream() whenever a change event has occurred in the file. The file is read successfully but to my understanding, the read content is interpreted as a plain text.

Is there any way to read the file as JS instead so that I can easily integrate the changes in the file to the server?

Here's what the config file looks like

config.js

var SERVER_CONFIG = {
    SSL_PATH: '/etc/letsencrypt/live/certificates/',
    PORT: { 
        HTTP: 5555,
        HTTPS: 5556
    }
}

if (typeof module !== "undefined") {
    module.exports.SERVER_CONFIG = SERVER_CONFIG;
}

Currently, how I use the config.js is as a module that is imported when the server starts.

eg

var SERVER_CONFIG = require('./config.js').SERVER_CONFIG;

var sslPath = SERVER_CONFIG.SSL_PATH;

http.listen(SERVER_CONFIG.PORT.HTTPS, function () {
    console.log('http listening on *:' + SERVER_CONFIG.PORT.HTTPS);
});
Ardi
  • 161
  • 1
  • 11
  • 1
    You can clear the `require` cache and re-`require` it. Really depends on how you were loading it in your server code in the first place. Also it probably will come down to restarting anyway, unless your application is written specifically in a way that allows injecting a new configuration at any time. Please tell us more or show some code. – Bergi Jul 22 '21 at 10:21
  • Do you use module loader? Maybe on file change you can load that file as module and then read exported settings / run exported functions. Or maybe it is a JSON file? – Tom HANAX Jul 22 '21 at 10:21
  • Hello, I updated the question to include the config.js file. – Ardi Jul 22 '21 at 10:26
  • 1
    Seem that this answer https://stackoverflow.com/a/16060619/10563591 and comments address same scenario – Tom HANAX Jul 22 '21 at 10:43

0 Answers0