title: Express and WebSocket listening on the same port
body: <p>I have an app.js which is used to trigger two events when some POST data are received: </p>
  
  <ol>
  <li>Insert POST data into a database</li>
  <li>Send a message to a client using a WebSocket</li>
  </ol>
  
  <p>Here is the <strong>app.js</strong> (only the important lines)</p>
  
  <pre><code>var express = require('express');
  var bodyParser = require('body-parser');
  var server = require('./server');
  
  var app = express();
  var port = process.env.PORT || 3000;
  
  app.post('/server', server);
  
  app.listen(port, function(){
    console.log('Slack bot listening');
  });
  </code></pre>
  
  <p>And here is the <strong>server.js</strong> (only the important lines)</p>
  
  <pre><code>var db = require('./DB');
  var WebSocketServer = require('ws').Server;
  
  var insertData = function(req, res){
  
      var wss = new WebSocketServer({server: server});
      console.log('WebSocketServer created');
      wss.on('connection', function(wss){
          wss.send(JSON.stringify('Socket open'));
      });
      wss.on('close', function(){
          console.log('WebServerSocket has been closed');
      });
  };
  
  module.exports = insertData;
  </code></pre>
  
  <p>What I would like to achieve is to set the WebSocketServer in a way that it listen to the same port of the app. 
  I thought about passing the <em>server</em> var from <strong>app.js</strong> to <strong>server.js</strong> but </p>
  
  <ol>
  <li>I think this a not an elegant way to do it</li>
  <li>I don't know how to do it</li>
  </ol>
  
  <p>What do you guys think? </p>