accessing required modules from other modules

Posted by john smith on Stack Overflow See other posts from Stack Overflow or by john smith
Published on 2012-12-07T15:48:09Z Indexed on 2012/12/07 17:06 UTC
Read the original article Hit count: 247

Filed under:
|

I have a bare-bone express application, exactly the one that is created with the express command.

I have installed socket.io and attached it to my server, like this:

var app = express(),
    server = http.createServer(app),
    io = io.listen(server);

server.listen(8000);

Now, I also have the routes files, which is called like this:

app.get('/', routes.index);

Inside this module I have the following function:

exports.index = function(req, res){
  socket.emit('news', { message: "foo" });
};

This obviously leads to a 500 reference error, because the routes file is an exportable module, and obviously has no idea what the socket is, as it is located in the app.js file.

Is there a way I can access this socket object from this, or any other file? Please note that it is attached to the express generated app.


extra: what about getting/setting session data?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about node.js

Related posts about module