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 https://openshift.redhat.com/app/console/applications 
  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: http://hapijs.com/tutorials)
    • 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/ 127.0.0.1 - this allows us to run/test the app locally.
    console.warn('No OPENSHIFT_NODEJS_IP var, using 127.0.0.1');
    ipaddress = "127.0.0.1";
};

server.connection({
    host: ipaddress,
    port: port});

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

// Start the server
server.start();

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