9

I am trying to deploy my node.js application (with express and mongoose) to openshift and I am not able to do so. The application works perfectly on my local environment.

my entry point is the file /bin/www

I establish this as the entry point on openshift with this line in the package.json file (per this thread):

"main": "bin/www",

I have made sure to set my mongodb connection using environment variables according to the guide like so:

// default to a localhost configuration:
var mongoConnectionString = 'mongodb://127.0.0.1/code-blog';

// if OPENSHIFT env variables are present, use the available connection info:
if (process.env.OPENSHIFT_MONGODB_DB_PASSWORD) {
  mongoConnectionString = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" +
  process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "@" +
  process.env.OPENSHIFT_MONGODB_DB_HOST + ':' +
  process.env.OPENSHIFT_MONGODB_DB_PORT + '/' +
  process.env.OPENSHIFT_APP_NAME;
}

mongoose.connect(mongoConnectionString);

The error that I get is:

remote: Waiting for application port (8080) become available ...
remote: Application 'codeblog' failed to start (port 8080 not available)
remote: -------------------------
remote: Git Post-Receive Result: failure
remote: Activation status: failure
remote: Activation failed for the following gears:
remote: 558a25bd5973ca7a74000162 (Error activating gear: CLIENT_ERROR: Failed to
 execute: 'control start' for /var/lib/openshift/558a25bd5973ca7a74000162/nodejs

remote: #<IO:0x00000000b49380>
remote: #<IO:0x00000000b49308>
remote: )
remote: Deployment completed with status: failure
remote: postreceive failed
To ssh://558a25bd5973ca7a74000162@codeblog-donaldsubert.rhcloud.com/~/git/codebl
og.git/
   29635a8..7a0e926  master -> master

This is peculior to me because I do not specify port 8080 anywhere. In fact, the default port is specified here:

var port = normalizePort(process.env.OPENSHIFT_NODEJS_PORT || "3000");
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";

app.set('port', port);

var server = http.createServer(app);

I am not really sure where to go from here. I don't seem to have enough information to determine my next step.

[edit] I added some logging to test what port this is running on, but the logging statement is never run. Here is the code

console.log("TEST TEST TEST");

var app = require('../app');
var debug = require('debug')('ProjectTemplate:server');
var http = require('http');

var port = normalizePort(process.env.OPENSHIFT_NODEJS_PORT || "3000");
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";

console.log("PORT: ", port);

and output

TEST TEST TEST
module.js:340
    throw err;
    ^
Error: Cannot find module './routes/logIn'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/var/lib/openshift/558a25bd5973ca7a74000162/app-root/           runtime/repo/app.js:26:13)

TEST TEST TEST is from a logging statement at the beginning of the entry point file. Something seems to fail before it hits the console.log("PORT: ", port); It is probable that this is something to do with app.js where the MongoDb connection is made.

[/edit]

Community
  • 1
  • 1
Don Subert
  • 2,636
  • 4
  • 27
  • 37
  • `console.log(port);` and see what it gives you – Ed Knowles Jul 20 '15 at 08:33
  • Post configuration of you application service for http traffic has nothing to do with MongoDB. Please stop adding irrelevant tags to your question – Blakes Seven Jul 20 '15 at 08:37
  • Similar error messages addressed on these boards point to an issue with the MongoDb connection string. I am unwilling to rule that out as a possible cause without a more substantial inquiry – Don Subert Jul 20 '15 at 08:38
  • Port `8080 is clearly the setting of your `OPENSHIFT_NODEJS_PORT`. Your error message is also not about MongoDB connections, but waiting for the service port for http. As in clearly. I can do this all day. We don't need the noise for a question that has nothing to do with the topic. – Blakes Seven Jul 20 '15 at 08:41
  • I am seriously going to report you for harassment – Don Subert Jul 20 '15 at 08:42
  • Blakes Seven. I am not saying definatively that this is a Mongo issue. It just looks like it might have been. Edward Knowles, I added the logging statement that you suggested but it never runs. I will edit the original post with more information, momentarily – Don Subert Jul 20 '15 at 09:04

6 Answers6

10

Had exactly same error message: Application 'appname' failed to start (port 8080 not available) on open shift node app

After a lot of reading found out that many different users came to different solutions for the same error message, including myself. So I'd advice not to look for quick solutions for this error. The most important step is step 1 in the below list.

My solution was to add a missing dependency in package.json, for my particular case I needed to add "bcrypt":"~0.8.5", such a stupid thing!

Now, how did I get to fix the issue only knowing the "port 8080 not available" error:

  1. ssh'd into the app, went to the app repo dir (cd $OPENSHIFT_REPO_DIR) and run npm start
  2. Got [...] Error: Cannot find module 'bcrypt' [...]
  3. Logged out from ssh, run npm info bcrypt | grep "version:", it returned "0.8.5"
  4. Added entry "bcrypt":"~0.8.5" to my package.json and commited/pushed changes.
  5. Problem solved, app runs!
aesede
  • 5,541
  • 2
  • 35
  • 33
  • 2
    I was trying to figure out how to get a third-party application working on my instance, the `npm start` recommendation on the ssh'd app gave me the tools to fix the issues I was having. Thanks! – Sienna Jan 09 '16 at 05:40
10

The error turned out to be a generic error that had nothing to do with the port. Apparently, if there is any fatal javascript error, it gives this message.

Don Subert
  • 2,636
  • 4
  • 27
  • 37
5

I have mentioned in detail here ... Application 'appname' failed to start (port 8080 not available) on open shift node app

The solution is you need to specify IP address when listening server.

This is how I have fixed it.

var express = require('express');
var app = express();
var http = require('http');

app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 3002);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");


http.createServer(app).listen(app.get('port') ,app.get('ip'), function () {
    console.log("✔ Express server listening at %s:%d ", app.get('ip'),app.get('port'));
    server();
});
Community
  • 1
  • 1
codebased
  • 6,945
  • 9
  • 50
  • 84
1

I don't think that this line will create the server on the specified port

var server = http.createServer(app);

You will need to tell it to listen on the port like so:

server.listen(port);
Matthew Wilson
  • 2,045
  • 14
  • 27
1

While your app is listening on the correct port, the thing missing is that you don't specify the IP address to listen on. Add something like the following below app.set('port', port);

app.set('ipaddr', server_ip_address);
MartinB
  • 231
  • 1
  • 5
0

For me it was the connection string. The one that was suggested right after you created the mongodb cartridge is not working: Connection URL: mongodb://$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT/'nodejs'

But from this article, by using the OPENSHIFT_MONGODB_DB_URL variable it worked.

if(process.env.OPENSHIFT_MONGODB_DB_URL){
  mongodb_connection_string = process.env.OPENSHIFT_MONGODB_DB_URL + db_name;

}

user523234
  • 14,323
  • 10
  • 62
  • 102