Skip to content

Instantly share code, notes, and snippets.

@ryanj
Last active November 22, 2017 19:55
Show Gist options
  • Save ryanj/5267357 to your computer and use it in GitHub Desktop.
Save ryanj/5267357 to your computer and use it in GitHub Desktop.
A Git-based workflow for making your existing Open Source node.js application OpenShift-compatible.

Want to deploy an existing node.js Github project on OpenShift?

Give this recipe a try:

Add our OpenShift starter-code to your existing GitHub-hosted node.js application code:

cd $YOUR_PROJECT_FOLDER # This script assumes that your project is a git repo, with a git remote hosted on github.com
git remote add upstream -m master [email protected]:ryanj/nodejs-custom-version-openshift.git
git pull -s recursive upstream master # merge in a basic OpenShift skeleton

This will merge in an .openshift folder containing a few OpenShift-specific files, and a basic package.json file to define additional metadata for your application.

Configure your application's package.json file

If your application already had a package.json file, you'll want to check it for merge conflicts. Your package.json file should have each of the following attributes defined: 'main', 'scripts', 'engines', 'name', 'description', and 'dependencies'.

Make sure that the main and scripts attributes are configured to initialize your app correctly, enabling support for starting your application via the npm start command.

dependencies may be defined here so that npm install can bootstrap your application. You also have the option of checking your modules into your local node_modeules folder.

Connect your back-end to the correct ports

This OpenShift node.js quickstart repo includes a basic "Hello World" express application. Feel free to delete our copies, or restore your previous versions of the index.html and server.js files.

OpenShift provides environment variables that tell your application which ip address and port numbers to bind to. Check your server side code and make sure that your application will optionally prefer to connect to OpenShift-defined ports whenever possible.

var http = require('http');
var ip = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1',
    port = process.env.OPENSHIFT_NODEJS_PORT || '8080';

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(port, ip);
console.log('Server running at http://'+ip+':'+port+'/');

Additional environment variables are available to your application as well. Binding to MongoDB, MySQL, or Postgres is easy if you've requested that they be available.

Resolve your merge conflicts

git status should help you find merge conflicts.

Resolve any remaining conflicts by deleting the duplicate code, reviewing your changes with git diff, and marking the conflicted files as resolved with git add. Then, commit and push your updated application code:

git add filename # after fixing, for each file that contained a merge conflict
git commit -m 'merging in OpenShift dependencies for node.js applications'
git push # to enhance your GitHub project, making it OpenShift compatible.

Finally, provision and deploy your new OpenShift-compatible node.js application, using your updated GitHub repo as the initial application source:

rhc app create mynodeapp nodejs --from-code=http://github.com/YOUR_GH_PROFILE/YOUR_GH_REPO.git

The rhc command-line output should return your live application URL.

If you are working with Open Source teams, this guide may also be of interest to you: https://www.openshift.com/blogs/secret-free-source-on-paas

@andresgottlieb
Copy link

You have a typo at the end of "Configure your application's package.json file", it says: "node_modeules"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment