0

I have my app.js set up like this:

app.js

 var express = require('express');
 var socket_io = require("socket.io");
 var app = express();
 
 //Our route variables.
 var routes = require('./routes/index');
 var users = require('./routes/users');
 var work = require('./routes/work');
 

 // Socket.io
 var io = socket_io();
 app.io = io;

 var playground = require('./routes/playground')(io);

 //CODE I WANT MOVED TO playground.js!
 //Socket IO playground
 io.on('connection', function(socket) {
   console.log('a user connected');
   socket.on('chat message', function(msg) {
     io.emit('chat message', {
       message: msg.message,
       nickname: msg.nickname
     });
   });

   socket.on('disconnect', function() {
     console.log('a user disconnected');
   });
 });

I was told I could send .io to my route playground.js like this:

var playground = require('./routes/playground')(io);

However I get the following error cannot call method of 'IndexOf' undefined

I do not want my socket.io code to be in app,js because I don't want app.js to become messy. (The code will grow overtime) I only want to use socket.io in playground.js because all of my test apps will be there. Please let me know if I can make this more clear

playground.js

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


module.exports = function(io) {
  io.on('connection', function(socket) {
    console.log("connection made!");
  });
};

router.get('/', function(req, res, next) {
  res.render('playground');
});

module.exports = router;
Tony
  • 351
  • 3
  • 19
  • Can you add `var playground....` to your `app.js` code. I want to see where you are calling it. – Zee May 20 '15 at 07:42
  • Is there a `module.exports` defined in the `playground` route? Does it take in an argument in the function definition? e.g. `module.exports = function (io) {...]` – Amin Shah Gilani May 20 '15 at 07:48
  • @amingilani Yes, but im getting the error before I even move into playground.js – Tony May 20 '15 at 07:49
  • I was editing my previous comment, before you answered, sorry. Does the function definition take the `io` argument? And you're certain that your path is correct? – Amin Shah Gilani May 20 '15 at 07:53
  • @amingilani Yes, the path is correct or the route wouldn't work and I'd get an error that the path could not be found. – Tony May 20 '15 at 07:55
  • @Tony, btw, pass the `app` in as well `require('./routes/playground')(app, io);` in `app.js` and add `app.use('/', router);` in the `playground` route. – Amin Shah Gilani May 20 '15 at 08:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78289/discussion-between-amingilani-and-tony). – Amin Shah Gilani May 20 '15 at 08:07

1 Answers1

1

In your playground.js the last line module.exports = router; supersedes the previous module.exports declaration. Try:

//routes/playground.js
var express = require('express');
var router = express.Router();


module.exports = function(app, io) {
  io.on('connection', function(socket) {
    console.log("connection made!");
  });
};

router.get('/', function(req, res, next) {
  res.render('playground');

app.use('/', router);
});

and try require('./routes/playground')(app, io); in your app.s

Amin Shah Gilani
  • 8,675
  • 5
  • 37
  • 79
  • debug (dispatching %s %s' req.method, req.url) ^ Cannot read property of method unefined – Tony May 20 '15 at 08:28