1

I'm struggling with Express.io, Express 4 and some routing stuff and I need your help!

The code posted here works.

My problem is that I'd love to separate the logic from app.js to every single route file (i.e. map.js). Unfortunately I can't find on the Internet any example with multiple route files and express.io using Express 4 and Router objects.

Can you please take a rapid look and address me to the right way to cope with it?

app.js

var express = require('express.io');
var app = express();
app.http().io();
// ...
var map = require('./routes/map');
app.use('/map', map);

app.io.route('ready', function(req) {
    setInterval(function() {
        req.io.emit('talk', {
            message: 'Random greeting'
        });
    }, 500);
});

/routes/map.js (this file uses standard Express 4 module)

var models = require('../models');
var path = require('path');
var express = require('express');
var router = express.Router();

router.get('/', function(req, res) {

    res.sendfile(path.resolve(__dirname + '../../client/mapClient.html'), function(err) {
        if(err) {
            console.log(err);
        }
    });
});

module.exports = router;

mapClient.html

<script src="/socket.io/socket.io.js"></script>
<script>
io = io.connect()

// Emit ready event.
io.emit('ready') 

// Listen for the talk event.
io.on('talk', function(data) {
    // do something with data.message
})  

</script>
Cob013
  • 1,057
  • 9
  • 22

0 Answers0