Muhammad Umar Muhammad Umar - 1 month ago 14
Node.js Question

Exporting and module exporting in node.js

I have a file on main root as


test_file.js


Following is the code inside it

var config = require('config');
var Ctrl = require('./Controllers');
var port = process.env.PORT || config.get("PORT") || 3000;
var cors = require('cors');
var express = require('express');
var app = express();
var router = express.Router();

app.use(cors());

app.use(router);
var server = require('http').createServer(app);

var io = require('socket.io')(server, {'pingInterval': 4000, 'pingTimeout': 6000});

io.on('connection', Ctrl.connection);

console.log("Opening at port " + port);
server.listen(port, function () {
});

module.exports = router;
require('./Routes')();


I have another file in path


/Controllers/index.js


i want to pass out
io
to index.js too, here is its code

var Promise = require('q').Promise;
var config = require('config');
var mysql = require('mysql');

/// I want to get var io = require('../test_file.js');

/**Initialization socket connection */
exports.connection = function (socket) {
if (!(socket.handshake.query.accountType != null && socket.handshake.query.id != null && socket.handshake.query.accessKey != null
&& socket.handshake.query.id > 0)) {
socket.disconnect();
return;
}


Now i am confused about module.exports and exports, my module.exports is already passing out to another file, i want to add another variable i-e
io
and pass it to controllers file. How can i do that

In respond to the query,

app.use(router);
var server = require('http').createServer(app);

var io = require('socket.io')(server, {'pingInterval': 4000, 'pingTimeout': 6000});
module.exports = {
router: router,
io: io
};

io.on('connection', Ctrl.connection);

console.log("Opening at port " + port);
server.listen(port, function () {
});

require('./Routes')();


In Controllers/index.js

var Promise = require('q').Promise;
var config = require('config');
var mysql = require('mysql');

var Driver = require('./driver');
var User = require('./user');

var io = require('../test_file.js').io;
console.log("logging");
console.log(io);


the result is
{} for io

Answer

exports is just module.exports's little helper. Your module returns module.exports to the caller ultimately, not exports. All exports does is collect properties and attach them to module.exports IF module.exports doesn't have something on it already. If there's something attached to module.exports already, everything on exports is ignored.

you can pass both as the object

 module.exports = {
  router: router,
  io: io
};

Please refer this doc for more explanation

Edit

Access object via module.exports

file.js

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = {router:router}; 

Acess it with

var router=require('./file').router
   OR

var router=require('./file');

router.router 
Comments