服务器使用 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);
});
});
});
我在用着:
快递 4.2.0
Primus 2.2.1