[英]node.js configure submodules
[編輯]
感謝Stafano,他以更好的方式將我的問題正式化了:您有一個模塊
-)此模塊中有幾個文件
-)所有這些文件都依賴於模塊本身未知路徑的配置
-)這個模塊本身並不能做很多事情,並且可以被其他應用程序使用
-)這些應用程序應該在使用模塊之前向其注入配置路徑
所以我有這個模塊,可以從另一個應用程序中使用。 它由其他子模塊組成,我想使用配置對象對其進行配置。 我已經嘗試將配置注入我的子模型中,但是原始問題中暴露了同樣的問題。
例如,我的模塊使用mongoDB(帶有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文件)。 我需要按需加載該配置,並將其公開給模塊。
我該如何實現這種行為?
就像是:
// 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
}
還有另一種獲取配置路徑,配置模塊並共享配置的方法嗎?
“共享配置”是指我想讓模塊的其他文件可以使用該配置。
感謝您的任何建議!
最終編輯:
經過許多誤會,我終於明白了您的問題。 總結評論中的內容,情況如下:
由於無法動態修改模塊導出的內容,因此應使用另一種方法。 與您在編程中遇到的大多數情況一樣,沒有一種方法總是對的,這取決於您的要求和限制。
執行此操作最簡單的方法(不建議這樣做)是使用全局變量,該變量是在myModule.js文件中設置的,並將由模塊中的其他文件使用。 這種方法的最大缺點是,您將無法同時使用不同配置的模塊的多個實例。 此外,任何其他模塊都可以通過簡單地更改全局變量的值來隨時輕松地(有意或無意)修改您的配置,因此這也存在安全風險。
更好的方法(這可能需要您做更多的工作-取決於您有多少文件)是實現某種控制反轉(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頁面上還有一個片段,您可以使用該片段僅使用本機方法來加載json配置。
編輯:我剛剛意識到,此模塊僅用於節點0.11,您可能沒有使用。 由於您可能不需要編寫功能,因此可以改用以下代碼段:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.