Reputation: 829
Started a project with npm which created a certain file structure:
www <-- require() calls app.js; instantiates server
app.js <-- instantiates var app = express(); and has module.exports = app;
Now, I'd like to use sockets.io. In my 'www' file, here is a code snippet:
var app = require('../app');
...
var server = http.createServer(app);
And I'd like to put all of my server-side socket listeners in app.js, but the following code:
var io = require('socket.io').listen(server);
requires server as an input. How do I make the server I instantiated in 'www' accessible in 'app.js'?
Upvotes: 2
Views: 2004
Reputation: 12882
It seems a little strange. But if you insist on such a structure than you can export an object from www
that will have app
as it's property and a method that binds socket listeners an receives app
object as a param.
module.exports = {
app: app,
bindSocketListeners: function(server, io) {
io.listen(server);
return io;
}
};
And call it:
var appObj = require('../app');
var io = require('socket.io');
var app = appObj.app;
var server = http.createServer(app);
io = appObj.bindSocketListeners(server, io)
Upvotes: 1