Express 4.2.0 server not working on Heroku while using primus

265 views Asked by At

The server works fine running locally with express and primus. However my routes fail when hosted on heroku. If I remove the primus wrapper around my routes it works both locally and on heroku.

var express = require('express'),
    Primus = require('primus'),
    Emitter = require('primus-emitter'),
    compress = require('compression'),
    mongoose = require('mongoose'),
    bodyParser = require('body-parser'),
    List = require('./models/list'),
    app = express(),
    port = process.env.PORT || 3000,
    router = express.Router();

/**
 * Load controllers.
 */

var apiController = require('./controllers/api');

/**
 * API keys.
 */

var secrets = require('./config/secrets');

/**
 * Mongoose configuration.
 */

mongoose.connect(secrets.db);
mongoose.connection.on('error', function() {
    console.error('✗ MongoDB Connection Error. Please make sure MongoDB is running.');
});

app
    .use(compress())
    .use(bodyParser())
    .use('/api', router)
    .use(express.static(__dirname + '/public'));

/**
 * server
 */

var server = require('http').createServer(app),
    primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });

primus.use('emitter', Emitter);

server.listen(port);
console.log('Server is listening on port ' + port);

primus.on('connection', function (spark) {
    var listsRoute = router.route('/list');

    listsRoute.get(function(req, res) {

        List.find(function(err, obj) {
            if (err) {
                res.send(err);
            }

            primus.send('obj', obj);
            res.json(obj);
        });
    });
});

Im using:

  • Express 4.2.0

  • Primus 2.2.1

1

There are 1 answers

0
Drastick On

I forgot to change the hostname primus was listening to on the client side from localhost to my heroku app url.