简体   繁体   中英

node.js dynamic module(file) export

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?

我建议将数据保存在JSON文件中,然后从文件中读取数据,而不是尝试将其用作模块。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM