我似乎无法通过引用Node模块传递对象。例如,在我的主server.js中,我有:
var test = {};
var ctrlFiles = require('./app/controllers/files')(test);
在server.js中,我拦截了一个HTTP请求,并更改了测试对象:
app.use(function(req, res){
console.log('2. updating test');
test.name = 'some value';
next();
});
在server.js中更进一步我定义路由并将请求传递给我的文件控制器:
app.get('/files', function(req, res) {
console.log('3. in files route, test is ', test);
ctrlFiles.getUserFiles(req, res);
});
文件控制器是:
module.exports = function(testReference) {
var test = testReference;
console.log('1. test is ', test);
return {
getUserFiles: function(req, res) {
console.log('4. in getUserFiles and test is ', test);
}
};
}
日志如下:
1. test is {}
2. updating test
3. in files route, test is { cookie:
{ name: 'some value' }
4. in getUserFiles and test is {}
当我对“/ files”执行GET请求时, test
始终为空。如何通过引用传递test
,这样当它在我的文件控制器之外更新时,它也会在控制器内更新?