Openshift

Helloworld NodeJS on Openshift

NodeJS provides an event-driven and asynchronous platform for server-side JavaScript. In this tutorial we will learn how to create and deploy a minimal Node application into Openshift.

Our NodeJS application barely requires a Javascript file named server.js which contains the application logic and a file named package.json to describe the project and dependencies, the application file itself, the public file directories, and a directory for routes.

You can check the starting project at: https://github.com/fmarchioni/ocpdemos/tree/master/nodejs-basic

Here is the simple server.js file:

var express = require('express'),
    fs = require('fs'),
    app = express();

var app = express();

var ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';


app.get('/', function(req, res) {
    res.send('Hello from NodeJS  at '+ new Date());
});


app.listen(8080, ip);

module.exports = app;

First, have declared the needed dependency to the Express framework. Express can be used to simplify nodejs application development as it doesn’t force application structure on the developer: you can place routes as public assets in any directory you want, and so oncall the res.write() method, which writes response data to the socket.

The last thing you need to do is bind to a port so you can listen for incoming requests. You do this by using the server.listen() method, which accepts a combination of Port and IP Address. The IP Address is declared in the variable ip, checking the environment or in the end relying in the default '0.0.0.0'.

Let's build the application with Openshift! Start it with "oc cluster up" then:

$ oc new-app https://github.com/fmarchioni/ocpdemos.git   --context-dir=nodejs-basic --name=nodejs-basic
--> Found image 6cc06d8 (4 weeks old) in image stream "openshift/nodejs" under tag "4" for "nodejs"

    Node.js 4 
    --------- 
    Platform for building and running Node.js 4 applications

    Tags: builder, nodejs, nodejs4

    * The source repository appears to match: nodejs
    * A source build using source code from https://github.com/fmarchioni/ocpdemos.git will be created
      * The resulting image will be pushed to image stream "nodejs-basic:latest"
      * Use 'start-build' to trigger a new build
    * This image will be deployed in deployment config "nodejs-basic"
    * Port 8080/tcp will be load balanced by service "nodejs-basic"
      * Other containers can access this service through the hostname "nodejs-basic"

--> Creating resources ...
    imagestream "nodejs-basic" created
    buildconfig "nodejs-basic" created
    deploymentconfig "nodejs-basic" created
    service "nodejs-basic" created
--> Success
    Build scheduled, use 'oc logs -f bc/nodejs-basic' to track its progress.
    Run 'oc status' to view your app.

Let's check that the service is available:

$ oc get services
NAME           CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
nodejs-basic   172.30.63.30   <none>        8080/TCP   6s

Finally, let's expose the route:

$ oc expose service nodejs-basic
route "nodejs-basic" exposed

Let's check the route:

oc get routes
NAME           HOST/PORT                                    PATH      SERVICES       PORT       TERMINATION
nodejs-basic   nodejs-basic-myproject.192.168.1.66.xip.io             nodejs-basic   8080-tcp   

And finally:

$ curl http://nodejs-basic-myproject.192.168.1.66.xip.io
Hello from NodeJS  at Fri May 26 2017 07:39:53 GMT+0000 (UTC)

Follow us on Twitter