title: Node with socket io, can not export the variable
body: <p>I'am trying to use <code>socket.io</code> in node, from router folder but I can not get to work variable <code>io</code>.</p>
  
  <p>This is my app.js file, where server is here;</p>
  
  <pre><code>var express = require('express');
  var path = require('path');
  var favicon = require('serve-favicon');
  var logger = require('morgan');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  var hbs = require('express-handlebars');
  var debug = require('debug')('to-moon:server');
  var http = require('http');
  //var fs = require('fs');
  var socket = require('socket.io');
  
  var index = require('./routes/index');
  
  
  var app = express();
  
  // Server Setup
  
  var port = normalizePort(process.env.PORT || '8000');
  app.set('port', port);
  
  var server = http.createServer(app);
  
  /**
   * Listen on provided port, on all network interfaces.
   */
  
  server.listen(port);
  server.on('error', onError);
  server.on('listening', onListening);
  
  var io = socket(server);
  
  
  // view engine setup
  app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts'}));
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'hbs');
  
  // uncomment after placing your favicon in /public
  //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  
  app.use('/js', express.static(__dirname + '/node_modules/bootstrap/dist/js')); // redirect bootstrap JS
  app.use('/js', express.static(__dirname + '/node_modules/jquery/dist')); // redirect JS jQuery
  app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css')); // redirect CSS bootstrap
  
  app.use('/', index);
  
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  
  // error handler
  app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
  
    // render the error page
    res.status(err.status || 500);
    res.render('error');
  });
  
  /**
   * Normalize a port into a number, string, or false.
   */
  
  function normalizePort(val) {
    var port = parseInt(val, 10);
  
    if (isNaN(port)) {
      // named pipe
      return val;
    }
  
    if (port &gt;= 0) {
      // port number
      return port;
    }
  
    return false;
  }
  
  /**
   * Event listener for HTTP server "error" event.
   */
  
  function onError(error) {
    if (error.syscall !== 'listen') {
      throw error;
    }
  
    var bind = typeof port === 'string'
      ? 'Pipe ' + port
      : 'Port ' + port;
  
    // handle specific listen errors with friendly messages
    switch (error.code) {
      case 'EACCES':
        console.error(bind + ' requires elevated privileges');
        process.exit(1);
        break;
      case 'EADDRINUSE':
        console.error(bind + ' is already in use');
        process.exit(1);
        break;
      default:
        throw error;
    }
  }
  
  /**
   * Event listener for HTTP server "listening" event.
   */
  
  function onListening() {
    var addr = server.address();
    var bind = typeof addr === 'string'
      ? 'pipe ' + addr
      : 'port ' + addr.port;
    debug('Listening on ' + bind);
  }
  
  
  module.exports = app;
  </code></pre>
  
  <p>Then I'am trying to use <code>var io</code> in the <code>index.js</code> file;</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  ..
  
  io.on('connection', function() {
    console.log('made connection');
    io.sockets('data', data);
  });
  module.exports = router;
  </code></pre>
  
  <p>But I'am getting <code>io is not defined</code> error.</p>