title: events.js:72 throw er; // Unhandled 'error' event ^ Error: listen EACCES on OpenShift
body: <p>I am trying to make a simple chat app. It <strong>works fine on localhost:3000/chat.html</strong>, but when it is deployed on <strong>OpenShift</strong>, it crashes with the following error: (After I <code>rhc tail</code> the log)</p>
  
  <p><strong>EDIT: I don't get this error any more</strong></p>
  
  <pre><code>events.js:72
          throw er; // Unhandled 'error' event
                ^
  Error: listen EACCES
      at errnoException (net.js:905:11)
      at Server._listen2 (net.js:1024:19)
      at listen (net.js:1065:10)
      at net.js:1147:9
      at dns.js:72:18
      at process._tickCallback (node.js:442:13)
      at Function.Module.runMain (module.js:499:11)
      at startup (node.js:119:16)
      at node.js:929:3
  DEBUG: Program node server.js exited with code 8
  DEBUG: Starting child process with 'node server.js'
  </code></pre>
  
  <p>I have read similar posts about this error, which didn't solve my problem. 1) I am binding at a high <code>port (3000)</code>
  2) My address is not in use (otherwise, I will get <code>EADDRRINUSE</code>) 3) I have another application on <em>OpenShift</em> that is listening to <code>port 3000</code>, but I think they shouldn't affect each other because the <em>domain</em> is different</p>
  
  <p>Here is my package.json:</p>
  
  <pre><code>{
    "name": "chat",
    "version": "1.0.0",
    "description": "",
    "main": "server.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
      "bcrypt-nodejs": "0.0.3",
      "body-parser": "^1.15.1",
      "cookie-parser": "^1.4.3",
      "express": "^4.13.4",
      "express-session": "^1.14.2",
      "multer": "^1.1.0",
      "sockjs": "^0.3.18"
    }
  }
  </code></pre>
  
  <p>Here is my server.js:</p>
  
  <pre><code>var express = require('express');
  var app = express();
  var session      = require('express-session');
  
  var http = require('http');
  var sockjs = require('sockjs');
  
  var connections = [];
  var chat = sockjs.createServer();
  chat.on('connection', function(conn) {
      connections.push(conn);
      var number = connections.length;
      conn.write("Welcome, User " + number);
      conn.on('data', function(message) {
  
          for (var ii=0; ii &lt; connections.length; ii++) {
              connections[ii].write("User " + number + " says: " + message);
          }
      });
      conn.on('close', function() {
          for (var ii=0; ii &lt; connections.length; ii++) {
              connections[ii].write("User " + number + " has disconnected");
          }
      });
  });
  
  var server = http.createServer();
  chat.installHandlers(server, {prefix:'/chat'});
  server.listen(9999, '127.0.0.1');
  
  app.use(express.static(__dirname + '/public'));
  
  var ipaddress = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
  var port      = process.env.OPENSHIFT_NODEJS_PORT || 3000;
  
  app.listen(port, ipaddress, function () {
      console.log( "Listening on " + ipaddress + ", port " + port )
  });
  </code></pre>
  
  <p>I have <strong>pinned down</strong> the error at this portion of the <code>server.js</code>, if I comment it out, the chat will obviously not work, but I can deploy it on <em>OpenShift</em>. Am I creating the <code>socket</code> or <code>HTTP</code> server wrong on <em>OpenShift</em>?</p>
  
  <pre><code>var connections = [];
  var chat = sockjs.createServer();
  chat.on('connection', function(conn) {
      connections.push(conn);
      var number = connections.length;
      conn.write("Welcome, User " + number);
      conn.on('data', function(message) {
  
          for (var ii=0; ii &lt; connections.length; ii++) {
              connections[ii].write("User " + number + " says: " + message);
          }
      });
      conn.on('close', function() {
          for (var ii=0; ii &lt; connections.length; ii++) {
              connections[ii].write("User " + number + " has disconnected");
          }
      });
  });
  
  var server = http.createServer();
  chat.installHandlers(server, {prefix:'/chat'});
  server.listen(9999, '127.0.0.1');
  </code></pre>
  
  <p><strong>EDIT:</strong>
  <s>
  Instead of:</p>
  
  <pre><code>var server = http.createServer();
  chat.installHandlers(server, {prefix:'/chat'});
  server.listen(9999, '127.0.0.1');
  </code></pre>
  
  <p>tried this and also didn't work on <strong>OpenShift</strong>, also works fine on <strong>localhost:3000/chat.html</strong></p>
  
  <pre><code>app.set('port', 9999);
  var server = http.createServer(app).listen(app.get('port'), function(){
      console.log('Express server listening on port ' + app.get('port'));
  });
  chat.installHandlers(server, {prefix:'/chat'});
  </code></pre>
  
  <p></s>
  <strong>EDIT:</strong>
  After I put this line all the way in the top</p>
  
  <pre><code>var ipaddress = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
  </code></pre>
  
  <p>Instead of:</p>
  
  <pre><code>var server = http.createServer();
  chat.installHandlers(server, {prefix:'/chat'});
  server.listen(9999, '127.0.0.1');
  </code></pre>
  
  <p>I have:</p>
  
  <pre><code>var server = http.createServer(app).listen(app.get('port'), ipaddress, function(){
      console.log('Express server listening on port ' + app.get('port') + ', ip: ' + ipaddress);
  });
  chat.installHandlers(server, {prefix:'/chat'});
  </code></pre>
  
  <p>Now, it doesn't crash any more, but the http server is not listening.</p>
  
  <p><strong>DEBUG INFO:</strong></p>
  
  <p>1) localhost:</p>
  
  <pre><code>Http server listening on port 9999, ip: 127.0.0.1
  Express server listening on 127.0.0.1, port 3000
  </code></pre>
  
  <p>OpenShit:</p>
  
  <pre><code>Http server listening on port 9999, ip: 127.5.69.129
  Express server listening on 127.5.69.129, port 8080
  </code></pre>
  
  <p>2) Just found out that, when I run the application locally, then both my <code>localhost</code> and <em>OpenShift</em> are working locally on my machine. If I use my phone to get on the same <em>OpenShift</em> link, the <code>http</code> server is not listening, so I cannot chat.</p>