Javascript 如何在节点中的两个文件之间共享模块专用数据?
我想要一个Node.js模块,它是一个包含多个文件的目录。我希望一个文件中的一些变量可以从另一个文件中访问,但不能从模块外部的文件中访问。可能吗 那么让我们假设以下文件结构Javascript 如何在节点中的两个文件之间共享模块专用数据?,javascript,node.js,Javascript,Node.js,我想要一个Node.js模块,它是一个包含多个文件的目录。我希望一个文件中的一些变量可以从另一个文件中访问,但不能从模块外部的文件中访问。可能吗 那么让我们假设以下文件结构 ` module/ | index.js | extra.js ` additional.js 在index.js中: var foo = 'some value'; ... // make additional and extra available for the external code module.
` module/
| index.js
| extra.js
` additional.js
在index.js
中:
var foo = 'some value';
...
// make additional and extra available for the external code
module.exports.additional = require('./additional.js');
module.exports.extra = require('./extra.js');
// some magic here
var bar = foo; // where foo is foo from index.js
// some magic here
var qux = foo; // here foo is foo from index.js as well
在extra.js中
:
var foo = 'some value';
...
// make additional and extra available for the external code
module.exports.additional = require('./additional.js');
module.exports.extra = require('./extra.js');
// some magic here
var bar = foo; // where foo is foo from index.js
// some magic here
var qux = foo; // here foo is foo from index.js as well
在additional.js中
:
var foo = 'some value';
...
// make additional and extra available for the external code
module.exports.additional = require('./additional.js');
module.exports.extra = require('./extra.js');
// some magic here
var bar = foo; // where foo is foo from index.js
// some magic here
var qux = foo; // here foo is foo from index.js as well
Additional和Extra正在实现一些业务逻辑(彼此独立),但需要共享一些不应导出的模块内部服务数据
我看到的唯一解决方案是从additional.js
和extra.js
创建另一个文件service.js
和require
。对吗?还有其他解决办法吗
我希望一个文件中的一些变量可以从另一个文件中访问,但不能从模块外部的文件中访问
是的,这是可能的。您可以将另一个文件加载到模块中,并将其交给一个特权函数,该函数提供从模块范围访问特定变量的权限,或者只将其交给值本身:
index.js:
var foo = 'some value';
module.exports.additional = require('./additional.js')(foo);
module.exports.extra = require('./extra.js')(foo);
extra.js:
module.exports = function(foo){
// some magic here
var bar = foo; // foo is the foo from index.js
// instead of assigning the magic to exports, return it
};
additional.js:
module.exports = function(foo){
// some magic here
var qux = foo; // foo is the foo from index.js again
// instead of assigning the magic to exports, return it
};
好的,您可以使用“全局”命名空间执行此操作:
//index.js
global.foo = "some value";
然后
//extra.js
var bar = global.foo;
你能把想要的东西传进来吗
//index.js:
var foo = 'some value';
module.exports.additional = require('./additional.js')(foo);
module.exports.extra = require('./extra.js')(foo);
//extra.js:
module.exports = function(foo){
var extra = {};
// some magic here
var bar = foo; // where foo is foo from index.js
extra.baz = function(req, res, next){};
return extra;
};
//additional.js:
module.exports = function(foo){
var additonal = {};
additional.deadbeef = function(req, res, next){
var qux = foo; // here foo is foo from index.js as well
res.send(200, qux);
};
return additional;
};
请添加一些代码作为示例来说明您的问题。您到底需要这些代码做什么?这个要求听起来很奇怪——模块外部的另一个文件不是也来自同一个文件吗?@MatthewGraves添加了一些代码和解释,“但不是来自模块外部的文件”