我需要重构这个节点应用程序,所以socket.io和express使用相同的端口,我可以部署它。它目前按原样运行,但除非所有东西都使用相同的端口,否则不会在Heroku上运行。
var http = require('http');
var socket = require('socket.io');
var socketServer = http.createServer(app);
var io = socket.listen(socketServer);
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var path = require('path');
app.set('view engine', 'jade');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
var routes = require('./config/routes')(app, io);
var timer = require('./server/timer')(app, io);
var server = app.listen(process.env.PORT || 5000, function () {
console.log('Server running on *:5000');
});
socketServer.listen(8000);
我也只是学习节点,因此非常感谢任何关于重构的提示。
已修改为符合建议的解决方案: (Express以这种方式工作,但它会破坏socket.io)
var express = require('express');
var app = express();
var serv = require('http').createServer(app);
var io = require('socket.io').listen(serv);
var bodyParser = require('body-parser');
var path = require('path');
app.set('view engine', 'jade');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
var routes = require('./config/routes')(app, io);
var timer = require('./server/timer')(app, io);
serv.listen(process.env.PORT || 5000, function () {
console.log('Server running on *:' + (process.env.PORT || '5000'));
});
以下是控制台中的新错误:
socket.io-1.3.4.js:2 GET http://localhost:8000/socket.io/?user=Trey&EIO=3&transport=polling&t=1424902016787-2
看起来像websockets失败所以它回到长轮询,这也失败了......
答案 0 :(得分:8)
我认为这应该有用。
var express = require('express')
var app = express()
var server = require('http').createServer(app)
var io = require('socket.io')(server)
server.listen(80);
Heroku还有一些关于这个主题的有用信息:heroku dev info websockets