服务器使用express和primus在本地运行良好。但是我在heroku上托管时路由失败。如果我删除路线周围的primus包装器,它在本地和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);
});
});
});
我正在使用:
Express 4.2.0
Primus 2.2.1
答案 0 :(得分:0)
我忘了更改主机名primus在客户端从localhost到我的heroku app url。