,对我来说,它看起来像你希望是这样的。如果我错误地理解了你的要求,请纠正我。
与要求()
context.js访问
var context = {};
context.userGateway = require('../path/to/userGateway/module');
module.exports.context = context;
=====================================================================================
//usage in reference file
var context = require('/path/to/context/file');
module.exports = {
find: function(id){
var user = context.userGateway.find(id);
}
};
无访问需要()
var context = {};
context.userGateway = require('../path/to/userGateway/module');
GLOBAL.context = context; // makes your context object accessible globally, just like, console, require, module etc.
=====================================================================================
//usage in reference file
module.exports = {
find: function(id){
var user = context.userGateway.find(id);
}
};
什么是真正的问题在这里?如何在nodejs中创建单例?模块在节点中已经是单身。你甚至可以从多个位置从'require()'返回相同的对象。 – rossipedia
是创建一个单身人士,并能够跨所有节点模块共享 – PositiveGuy
好,所以它实际上只是创建一个需求也许是一个JSON对象的属性指向其他要求? – PositiveGuy