title: How to use HTTPS on Node.js using Express/Socket.io
body: <p>Im trying to run my node server with https. I'm using express and socket.io.</p>
  
  <p>This is my code for https:</p>
  
  <pre><code>var httpsPort = 443;
  var privateKey = fs.readFileSync(mykeypath');
  var certificate = fs.readFileSync(mycertificatepath');
  var credentials = {key: privateKey, cert: certificate};
  var https = require('https').Server(credentials,app);
  var io = require('socket.io')(https);
  
  https.listen(httpsPort, function(){
  logger.info('listening on *:' + httpsPort);
  });
  
  
  app.get('/initGame', function (req,res){
  
  var slots = require('./slots.json', 'utf8');
  var userObject = {
      address : req.connection.remoteAddress,
      userAgent : req.headers['user-agent']
  };
  db.getPlayedGames(userObject,function(playedGames){
      logger.debug(playedGames);
      if(typeof playedGames == 'undefined' ){
          playedGames=0;
      }else{
          playedGames = playedGames.games_played;
      }
      var spinsLeft = 10-playedGames;
      res.json({
          spinsLeft: spinsLeft,
          slots: slots
      });
    });
  });
  </code></pre>
  
  <p>on my client its the following:</p>
  
  <pre><code>var myServer = "//" + document.domain + ":443";
  
  $.get( myServer + "/initGame", function(data) {
      totalSpinsLeft = data.spinsLeft;
      $('#trysLeft').text(totalSpinsLeft);
      Seven.init(data.slots);
  }).fail(function(){
      setTimeout(function(){
          $('#spinner2').text('Fehler bitte neu laden!');
      },3000);
  
  });
  </code></pre>
  
  <p>Right now im getting the following exception on my server:</p>
  
  <p>uncaughtException: Missing PFX or certificate + private key. </p>
  
  <p>EDIT: right now im getting</p>
  
  <p>Bad Request</p>
  
  <p>Your browser sent a request that this server could not understand.
  Reason: You're speaking plain HTTP to an SSL-enabled server port.
  Instead use the HTTPS scheme to access this URL, please.</p>