简体   繁体   English

node.js动态模块(文件)导出

[英]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. modulefile已正确实时保存。

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. 创建一个名为getVariable的函数,然后简单地返回变量。

Include getVariable in your main module. 在您的主模块中包含getVariable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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