Reputation: 841
I want to export the socket.io object from my app.js of express
My app.js file is here: http://pastebin.com/Kfny4yVK
My bin/www file is here: http://pastebin.com/qGhPm6KE
In my app.js I do:
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
exports.io = io;
In a routes file I do:
var io = require(__dirname+'/../app.js').io;
However, when I call the:
io.on
I get an undefined object error. Any idea why this is happening? It looks like nodejs is somehow modifying the io object at export? Is this possible? Is there a way to make this work?
Upvotes: 0
Views: 332
Reputation: 9454
With Express v4.x I use the following to export the socket.io object.
APP.JS
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
//Export sio module so socket.io can be used in other modules
module.exports.sio = io; //ADDED this
OTHER_FILE.JS
//Import sio module from app.js
var io = require('../app.js').sio;
Upvotes: 2