在我的 app.js 文件中我会有类似的内容
var obj = {one : 1}
然后我会在 app.js 文件中执行类似的操作:
app.get("/index", middleware, 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};
关于javascript - 从 app.get 回调调用的模块中的主文件访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41150586/