Saturday 7 February 2015

Hapijs in Openshift

Today I tried to host a small Hapijs app in Openshift, as it may be useful for others just documenting here,

Creating new Gear with Nodejs cartridge in Openshift

  1.  Goto 
  2. Click Add application and add nodejs cartridge
Create a Hapijs Application

  1.  clone the project just created in openshift. 
  2.  delete all the files/folders except .openshift folder (note: make sure files deleted in git as well)
  3.  create new hapijs project on the same folder (refer:
    • npm init
    • npm install --save hapi
  4. create a file called index.js with following content
#!/bin/env node
var Hapi = require('hapi');

// Create a server with a host and port
var server = new Hapi.Server();

var ipaddress = process.env.OPENSHIFT_NODEJS_IP;
var port      = process.env.OPENSHIFT_NODEJS_PORT || 8080;

if (typeof ipaddress === "undefined") {
    //  Log errors on OpenShift but continue w/ - this allows us to run/test the app locally.
    console.warn('No OPENSHIFT_NODEJS_IP var, using');
    ipaddress = "";

    host: ipaddress,
    port: port});

// Add the route
    method: 'GET',
    handler: function (request, reply) {
        reply.file('Hello World!!!');

// Start the server

Test it locally and push it to openshit, enjoy coding.