var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var midi = require('midi');
var port = 80;


// Midi Stuff
// Set up a new input.
var input = new midi.input();
var output = new midi.output();

// Get the name of a specified input port.
console.log('Opening midi port:', input.getPortName(1));
input.openPort(1);
output.openPort(1);


server.listen(port, function () {
  console.log('Server listening at port %d', port);
});

// Routing
app.use(express.static(__dirname + '/public'));

io.on('connection', function (socket) {

  // when the client emits 'new message', this listens and executes
  socket.on('midiout', function (data) {
    console.log('Midi Output:', data);
    output.sendMessage(data);
  });

  // Configure a callback.
  input.on('message', function(deltaTime, message) {
    socket.emit('midiin', message);
  });

});

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var midi = require('midi');
var debug = false;
var httpPort = 80;
var midiPort = 1;


// Midi Stuff
// Set up a new input.
var input = new midi.input();
var output = new midi.output();

// Get the name of a specified input port.
console.log('Opening midi port:', input.getPortName(midiPort));
input.openPort(midiPort);
output.openPort(midiPort);


server.listen(port, function () {
  console.log('Server listening at port %d', port);
});

// Routing (for index.html)
app.use(express.static(__dirname + '/public'));

io.on('connection', function (socket) {

  // When we recieve a midiout event, push that to the midi port
  socket.on('midiout', function (data) {
  	debugMessage('OUTPUT', data);
    output.sendMessage(data);
  });

  // Emit a socket for input from the piano to clients
  input.on('message', function(deltaTime, message) {
  	debugMessage('INPUT ', message);
    socket.emit('midiin', message);
  });

});

// Helper func for message debug
function debugMessage() {
  if (debug) console.debug(arguments);
}
Refactor server and add simple debug mode
Refactor server and add simple debug mode