0

I created node.js project, with express and angularjs and mongodb. How can i deploy it to Openshift the project structure look like this

My user name for the openshift is admin password is X5900XJSLW4 db name is : topic

Now these are just fake , but they look like this. thanks

THIS WORK LOCALLY VERY FINE, BUT I GET Service Temporarily Unavailable @ http://topic-aggregator16.rhcloud.com/

error log found her https://github.com/chihabSD/TopicAggregator/blob/master/images/errorlog.png

I REALLY NEED THIS TO WORK ASAP

    config.db.mongo = {};
config.web.port = process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || process.env.WEB_PORT || 8080;
config.web.ip = process.env.OPENSHIFT_NODEJS_IP || process.env.IP;
config.db.mongo.url = process.env.OPENSHIFT_MONGODB_DB_URL || 'mongodb://localhost/topic';
3
  • Only the connection part, i am worried about, not thing else, and its working fine locally Commented Apr 9, 2016 at 20:56
  • I got waiting for connection " port 27017 and authnticate admin db, usin the rhc tail, any help Commented Apr 11, 2016 at 0:10
  • look @ the code again, i have just included the connection section, this is really taking so long. thank in advance My user name for the openshift is admin password is X5900XJSLW4 db name is : topic Commented Apr 11, 2016 at 0:11

2 Answers 2

5

To deploy an existing project into openshift you need to modify your package.json and server.json files:


package.json

  • Add the server.js file to the main key
  • Add a start script in the scripts key

    "main": "server.js", "scripts": { "start": "node server.js", }

example

{
      "name": "to-do",
      "version": "1.0.0",
      "description": "Simple todo app",
      "main": "server.js",
      "scripts": {
           "start": "node server.js",
           "test": "echo \"Error: no test specified\" && exit 1"
       },
      "keywords": [
          "node",
          "angular"
      ],
      "dependencies" : {
          "express"    : "~4.7.2",
          "mongoose"   : "~3.6.2",
          "morgan"     : "~1.2.2",
          "body-parser": "~1.5.2",
          "method-override": "~2.1.2"
      },
      "author": "[email protected]",
      "license": "MIT"
}

server.js

  • Add dynamic port from the openshift env
  • Add dynamic IP from the openshift env

    var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
    
    var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
    
    app.listen(server_port, server_ip_address, function () {
    
        console.log( "Listening on " + server_ip_address + ", server_port " + server_port  );
    
    });
    

Now you can use the rhc --from-code flag to deploy a project from existing code

rhc app create appName nodejs-0.10 -s --from-code=https://github.com/username/app.git

UPDATE

If you're using mongodb you need to update your server.js file

if(process.env.OPENSHIFT_MONGODB_DB_PASSWORD){
  connection_string = 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;
  config.db.mongo.url = connection_string;
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

7 Comments

that what i have right now, i just dont understand the idea behind openshift , its working fine locally but i did pushed it to openshift, but i got service not available or something like that.
the server now, i listening to it via port 3000, do i delete that,with code you sent me, or leave as its.
i used your code, but i got waiting for connection @ port 21017 and authnticate admin db etc. i used rhc tail, to view error log, i also used the code supplied above, but no luck, my db as username, password, and its own name in openshift,and thats why i am facing trouble deploying it.
I see, you must be using mongodb, in that case can you please post your error logs? Use this command to grab your log - rhc tail -a myapp
|
0

In general, OpenShift Online (V2) will publish connection information to your application using environment variables (https://developers.openshift.com/languages/nodejs/environment-variables.html#listen).

Your nodejs webservice will need to use this information to connect to OpenShift's load-balancers.

For this example code, you would need to change

config.web.port = process.env.PORT || process.env.WEB_PORT || 3000;
config.web.ip = process.env.IP;

to:

config.web.port = process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || process.env.WEB_PORT || 3000;
config.web.ip = process.env.OPENSHIFT_NODEJS_IP || process.env.IP;

For more information, see https://developers.openshift.com/languages/nodejs/environment-variables.html#listen

Npm modules cloud-env or config-multipaas may also be helpful if you prefer to stick with simpler, vendor-neutral application config strings (normalized to PORT and IP)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.