2

how can I exports this dynamic module?

// ./data/example5.js
module.exports = {title : example5, recentCheck : "2018-08-22"}


The contents change in real time. And I execute the followed function once a minute and connect the module.

    var files = fs.readdirSync(`./data/`);
    var i = 0;
    var link = [];

    while(i<files.length){ // read all file

        link[i] = require(`../data/${files[i]}`);

 

//main.js
setInterval(start,10000);  

I try to create and connect a new module file once a minute, but the first stored file(module) is extracted. The modulefile is being saved in real time correctly.

Shutting down and running the node will extract the changed modules correctly.

How do I handle a dynamically changing module?

sycoding
  • 39
  • 3

2 Answers2

1

I would recommend saving the data in a JSON file and then reading the data from the file rather than trying to use it as a module.

RedJandal
  • 1,203
  • 10
  • 18
1

Just make the objects you're updating variables in the module that you're including. Make a function called getVariable, and simply return the variable.

Include getVariable in your main module.

LJD
  • 498
  • 4
  • 11