Answer the question
In order to leave comments, you need to log in
How to make io scope?
/index.js
var express = require('express')
, app = express()
, path = require('path')
, server = require('http').createServer(app)
, io = require('socket.io')(server)
, mod = require("./mod")
mod.run();
server.listen(8080);
app.use(express.static(path.join(__dirname, 'public'));
mod = {
run:()=> {
//Как тут использовать io?
}
}
module.exports = mod;
Answer the question
In order to leave comments, you need to log in
mod.js
module.exports = class Mod {
constructor({ io }) {
this.io = io;
}
run() {
this.io.something();
}
}
var express = require('express')
, app = express()
, path = require('path')
, server = require('http').createServer(app)
, io = require('socket.io')(server)
, Mod = require("./mod")
const mod = new Mod({ io });
mod.run();
server.listen(8080);
app.use(express.static(path.join(__dirname, 'public'));
And cho, to transfer as parameter is not present, in any way?run: (io) => {
In principle, you can move the loading of io to the mod.js file, but on the condition that these variables are not used elsewhere.
If this is not possible, then you need to transfer already created variables to other components during initialization.
For example, create mod as a class and pass io to the constructor. Or call mod.run(io) and get it in the run function.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question