Javascript 从app.get回调调用的模块中的主文件访问变量
在我的app.js文件中Javascript 从app.get回调调用的模块中的主文件访问变量,javascript,node.js,express,module,Javascript,Node.js,Express,Module,在我的app.js文件中 module.exports = function(req, res){ but how would I access the variable obj from the main file } var obj={one:1} 然后我会在app.js文件中执行类似的操作: app.get(“/index”,中间件,require(“/mymodule”) 在我的模块中,我会做如下事情 module.exports = function(req, res){ bu
module.exports = function(req, res){
but how would I access the variable obj from the main file
}
var obj={one:1}
然后我会在app.js文件中执行类似的操作:
app.get(“/index”,中间件,require(“/mymodule”)
在我的模块中,我会做如下事情
module.exports = function(req, res){
but how would I access the variable obj from the main file
}
编辑:我喜欢将app.get放在主文件中,因此一种解决方案模式如下:
app.get("/reviewfor/:comp", checkIfAuthed, function(req, res){
require("./reviewforHandler")(req, res, obj);
})
但我想像上面那样做
另一个解决方案是在主文件中执行module.exports.obj
,但从主路由文件执行module.exports很奇怪。不是吗?试试看
var obj = require('./obj.js');
在app.js
和mymodule.js
中。然后在obj.js中:
module.exports = {one : 1};
试一试
在app.js
和mymodule.js
中。然后在obj.js中:
module.exports = {one : 1};