我无法在expressJs中的其他模块中导入app.js.它导入但我无法使用app.js文件中定义的函数
我的代码是 - 我有这个 app.js
var app = express();
var expressWs = require('express-ws')(app);
.
.
.
wss= expressWs.getWss();
// console.log(wss); // works fine
app.getWss=function(){
return "hello";
};
app.listen(3001);
module.exports = app;
在/socketroutes/socketroutes.js内的文件中
var app = require('../app');
console.log(app); // prints an empty object {}
console.log(app.getWss()) // returns undefined function and doesn't work
我想在另一个模块中使用app.js中定义的一些变量或函数。因为websocket服务器的实例应该只创建一次。我不能在每个模块中创建wss的实例。
答案 0 :(得分:0)
这主要是因为循环依赖。 尝试将module.exports行移动到app.js文件的开头。