F
F
fanhypermax2018-04-13 17:04:53
JavaScript
fanhypermax, 2018-04-13 17:04:53

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.js
mod =  {
      run:()=> {
   
      //Как тут использовать io?

      }
}

module.exports = mod;

How to make it possible to use io in mod.run?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladlen Hellsite, 2018-04-13
@fanhypermax

mod.js

module.exports = class Mod {
  constructor({ io }) {
    this.io = io;
  }

  run() {
    this.io.something();
  }
}

index.js
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'));

0
0xD34F, 2018-04-13
@0xD34F

And cho, to transfer as parameter is not present, in any way?
run: (io) => {

I
Ivan, 2018-04-13
@LiguidCool

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 question

Ask a Question

731 491 924 answers to any question