deploy nodejs angularjs mongodb expressjs application to openshift

In my previous post, I described how to upload file using nodejs and angularjs.

Now we are to deploy this MEAN stack app to openshift which is a very good cloud service provider offering 3 application deployment for free. You can even deploy Java web application to it using Tomcat/Mysql, part of which i mentioned in a previous post.

We first need to create a instance in openshift ,then we pull the source code to our local and merge with our local git, after fixing some conflict, we push our code to openshift. This whole process is in this preivous post.

Once we have the codebase and push it to server git, openshift will run npm towards our package.json every time we push something new there, very neat.

Some tricks needed

1. openshift ip and port

openshift has its own ip and port so we need to set it in our server.js when we start our app:

 

//openshift port or local port
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT ||3000;

app.listen(port, ipaddress, function () {
    logger.info('Express server listening on port: ' + port);
});

I use || so that the codebase would still run in my local. As for the variables in openshift, you can ssh to the openshift server and type:

env | grep OPENSHIFT_NODEJS

to see a bunch of environment vars set related to nodejs. IP and PORT are two of them.

2. mongo db connection

Similar to the ip and port, you also need some customization on the mongo database connection. I use mongoose. The below code would make sure our code works both locally and remotely.

 


var mongoose = require('mongoose');

// default to a 'localhost' configuration:
var connection_string = '127.0.0.1:27017/contacts';
// if OPENSHIFT env variables are present, use the available connection info:
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;
}

mongoose.connect('mongodb://' + connection_string);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));

console.log('Hey, database Node module is loaded')

module.exports = db;

3. mongo db data export and import to openshift

To export the local data, just execute command

mongoexport -d targetDB -c targetCollection -o targetFileName.json

This would generate the output file in the current directory.  Next we want to import this into the openshift mongo db. The tmp directory is shorthand for /tmp. On Linux, it’s a directory that is cleaned out whenever you restart the computer, so it’s a good place for temporary files.

So, we could do something like:

$ rsync targetFileName.csv openshiftUsername@openshiftHostname:/tmp
$ ssh openshiftUsername@openshiftHostname
$ mongoimport -d targetDB -c targetCollection--type csv /tmp/targetFileName.csv --headerline

4. bower install

If you are also using bower to manage the client dependencies like me, then you also need to run bower install to get all the client packages. running it directly from the console would not work in openshift since Bower expects to be able to write some of its config files in $HOME/.bower/ (or inside .config), but OpenShift does not provide write access to $HOME.

You can work around this issue by prepending HOME=$OPENSHIFT_DATA_DIR before the bowercommand whenever you run it:

HOME=$OPENSHIFT_DATA_DIR bower

This thread posts some methods. However a easy way I found is adding this into the package.json’s scripts using its postinstall phase:

  "scripts": {
    "postinstall": "export HOME=$OPENSHIFT_REPO_DIR; ./node_modules/bower/bin/bower install"
  },

This will setup a new HOME directory – that is writable by you as well as automatically invoke bower install.

Advertisements

4 comments

  1. dineshramitc · March 5, 2015

    Reblogged this on Dinesh Ram Kali..

  2. chris31389 · July 5, 2015

    Hi,

    I’ve been trying to get the postinstall script to work on windows locally. Do you have any ideas on how I can do this?

    Thanks

    • LEON · July 5, 2015

      Not sure why it is not working for you. In local env, it should be even easier as long as the npm could find the bower path.

      • chris31389 · July 6, 2015

        Thanks for the reply. It took me ages to find the simple solution. I ended up using an or operator.

        “postinstall”: “HOME=$OPENSHIFT_REPO_DIR bower install || bower install”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s