【问题标题】:node.js configure submodulesnode.js 配置子模块
【发布时间】:2015-01-29 23:26:38
【问题描述】:

[编辑]

感谢 Stafano 以更好的方式将我的问题正式化: 你有一个模块

-) 这个模块中有几个文件

-) 所有这些文件都依赖于模块本身未知路径的配置

-) 这个模块本身并没有做太多的事情,并且打算被其他应用程序使用

-) 这些应用程序应该在模块使用之前将配置路径注入到模块中

所以我有这个模块,从另一个应用程序中使用。它由其他子模块组成,我想使用配置对象对其进行配置。 我已经尝试在我的子模型中注入配置,但我在原始问题中遇到了同样的问题。

例如我的模块使用 mongoDB (with mongoose) 作为存储。

// app.js
// in the config object i have the URI to the mongo instance (in order to create a connection).
var myModule = require('myModule')(config);

// myModule.js
// files
// myModule/index.js expose the module's functionalities
// is the entry point so I create the mongoose connection
var mongoose = require('mongoose');
module.exports = function(config){
  var connection = mongoose.createConnection(config.store.URL);
  // I need to expose this connection to the others submodules.
}

// myModule/storeController.js contains the business logic that use the store (createItem, deleteItem, get...) and requrie mongoose and my Models (store in the models folder)
var mongoose = require('mongoose');
var Item     = require('./models/item.js');

exports.createItem = function(item){
  Item.save(item, function(err, item){
    if (err) throw
    ...
  });
} 

// myModule/models/item.js

// In this module i need to use the connection application in the configuration.
var mongoose = require('mongoose');
var connection = // i don't know how to get it

var ItemSchema = new mongoose.Schema({
  name: String
});

module.exports = mongoose.model('item', ItemSchema);

如果我将配置 obj 注入到 item.js 中,我将无法执行我的模型的 module.exports。 我希望这个例子可以澄清我的问题,但问题很简单,将对象作为参数后暴露。

[上一个] 我有一个需要模块的 node.js 应用程序。此模块接受配置文件路径(JSON 文件)。 我需要在 require 上加载该配置并将其公开给模块。

我怎样才能实现这种行为?

类似:

// app.js
var myModule = require('myModule')(__dirname + '/config/myModuleCnfig.json');

// myModule.js

module.exports = function(configPath){
  var config = require(configPath);
  module.exports = config;  // This is wrong
}

有没有其他方式获取配置路径,配置模块,共享配置?

“共享配置”是指我想让我的模块的其他文件可以使用该配置。

感谢您的任何建议!

【问题讨论】:

  • 如果你需要返回配置,为什么不在第一个要求之前将它放在一个变量中,然后将值传递给模块?
  • 我可以做到,但我后来也遇到了同样的问题,我找不到一种简单的方法将该配置暴露给我模块的其他文件,例如在配置中我可以有一些东西我在模块的另一个文件中使用,所以我试图使用 require 导出配置。你有什么建议吗??谢谢!
  • 我不明白。该模块是完全多余的。相当于只设置 var myModule = require(__dirname + '/config/myModuleCnfig.json');
  • 相对路径在 require() 中工作,所以你根本不需要 __dirname。
  • 它只是模块的第一行,接受配置文件路径的 index.js,这样做之后我如何将该配置暴露给我模块中的其他文件?示例: // myModule/index.js module.exports = function(config){ //something to share my configuration object } // 我的模块中的另一个文件 myModule/anotherOne.js var configuration = require what?

标签: javascript node.js configuration module


【解决方案1】:

最终编辑:

经过多次误解,我终于明白了你的问题。总结一下 cmets 中的内容,情况如下:

  • 你有一个模块
  • 此模块中有多个文件
  • 所有这些文件都依赖于模块未知路径的配置 本身
  • 这个模块本身并没有做太多的事情,它的目的是 由其他应用程序使用
  • 这些应用程序应该注入一个 模块使用前的配置路径

由于您无法动态修改模块导出的内容,因此您应该使用另一种方法。与您在编程中遇到的大多数情况一样,没有一种方法总是正确的,这取决于您的要求和限制。

执行此操作的最简单方法(我不推荐)是使用您在 myModule.js 文件中设置的全局变量,该变量将被模块中的其他文件使用。这种方法的最大缺点是您无法同时使用具有不同配置的模块的多个实例。此外,任何其他模块都可以随时轻松修改(有意或无意)您的配置,只需更改全局变量的值,因此这也是一个安全风险。

一个更好的方法是实现某种Inversion of Control (IoC),这可能需要您做更多的工作(取决于您拥有多少文件)。在您的情况下,您可以将所有导出转换为接受配置的函数,然后在需要模块后通过传递活动配置来初始化它们。我不知道你的实现细节,所以这里有一些示例代码:

// submodule1.js
module.exports = function(config) {
    // return something that uses the configuration
}

// myModule.js
var fs = require('fs');
var submodule1 = require('./submodule1');
var submodule2 = require('./submodule2');
// ...
module.exports = function(configPath){
    var config = JSON.parse(fs.readFileSync(configPath));
    var sm1 = submodule1(config);
    var sm2 = submodule2(config);
    return /* an object that uses sm1 and sm2 */; 
}

如果您的模块非常复杂,您可以使用一些 IoC 库来为您进行绑定。一个好的可能是Electrolite

希望这会有所帮助。

以前的答案:

您可以使用名为 jsop 的库:

var jsop = require('jsop');
var config = jsop('./config/myModuleCnfig.json');

如果您不想为此模块添加依赖项,链接的 GitHub 页面也有一个 sn-p,您可以使用它来仅使用本机方法加载 json 配置。

编辑:我刚刚意识到这个模块仅适用于节点 0.11,您可能没有使用它。由于您可能不需要写入功能,因此可以使用以下 sn-p 代替:

var fs = require('fs')
var config = JSON.parse(fs.readFileSync('./config/myModuleCnfig.json'))

编辑 2:

现在我想我更了解你的问题了。要将路径传递给所需的配置,您可以执行以下操作:

// myModule.js

var fs = require('fs')
module.exports = function(configPath){
    var config = JSON.parse(fs.readFileSync(configPath))
    return config;
}

【讨论】:

  • 嗨!我的问题不同,或者我没有正确理解您的答案。该模块由多个文件组成。我需要所有这些配置。所以我试图将它暴露给模块的每个文件。任何想法??谢谢
  • 只需将 sn-p 放在需要配置的每个文件的顶部...还是我遗漏了什么?
  • 你错过了我不知道路径:)
  • 好吧,也许我开始明白了……所以你只想要求一个模块根据动态模块传递的路径返回不同的配置,对吧?
  • 我刚刚再次编辑了我的答案,以解决我理解的问题。这就是你要找的东西吗?
猜你喜欢
  • 2016-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-10
相关资源
最近更新 更多