Perform file Reload without Restart

Asked

Viewed 42 times

-1

You can perform the Reload of the file without Restart of the application: - I have a file of config.json, this file is changed sometimes, only the API does not identify this new configuration, only after the application remains.

nconf.argv()    
    .env()
    .file('package', 'package.json')    
    .file({
        file: path.join(__dirname,'../caminho', process.env.NODE_ENV + '.json')        
    });

1 answer

1

You can use the function fs.watchFile to verify changes in this file and then perform the necessary action:

const path = request('path');
const fs = request('fs');

const CAMINHO = path.join(__dirname,'../caminho', process.env.NODE_ENV + '.json');

fs.watchFile(CAMINHO, (atual, anterior) => {
  console.log(`O horário de modificação anterior é: ${anterior.mtime}`);
  console.log(`O horário de modificação atual é: ${atual.mtime}`);
});

fs.watchFile

Watch for changes on filename. The callback Listener will be called each time the file is accessed.

In free translation:

Observe the changes in the file name. The callback Listener will be called whenever the file is accessed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.