Node.js - Using Socket.io and Express on the same port

I need to reorganize this node application so that socket.io and express use the same port and I can deploy it. It currently works as it is, but will not work on Heroku unless everyone uses the same port.

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);

I'm just learning node, so any refactoring tips in general would be greatly appreciated.

Edited in accordance with the proposed solution: (Express works this way, but it interrupts 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'));
});

Here's the new error in the console:

socket.io-1.3.4.js: 2 GET http: // localhost: 8000 / socket.io /? user = Trey & EIO = 3 & transport = polling & t = 1424902016787-2

The ports seem to be down, so they are returning to a lengthy survey, which also fails ...

+4
1

, .

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

+7

All Articles