Node.js Passing Object From Server.js To External Modules?
I have these declarations at the top of my server.js file var express = require('express'), app = express(), server = require('http').createServer(app), mongoose = requ
Solution 1:
In socketEvents.js
functionsockets(server) {
var io = require('socket.io')(server),
matchMakingQueue = [];
// etc...
}
module.exports = sockets;
In server.js:
var sockets = require('./socketEvents'),
express = require('express'),
app = express(),
server = require('http').createServer(app),
...
...
sockets(server);
Basically you export the function from where you want to use them and then require those files in server.js.
Sample project:
server.js:
var sockets = require('./socketEvents')
server = require('http');
sockets(server);
socketEvents.js:
functionsockets(server) {
console.log("Hello");
console.log(server);
}
module.exports = sockets;
Both files are in the same folder. To run: node server
Solution 2:
Wrap it up in a function that you should export.
functionwrap(server){
var io = require('socket.io')(server),
matchMakingQueue = [];
io.... //rest goes here
}
module.exports = wrap;
Post a Comment for "Node.js Passing Object From Server.js To External Modules?"