How to run 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.

NodeJS Overview

Node.js is an open-source, server-side JavaScript runtime environment that allows developers to execute JavaScript code outside of a web browser. It is known for its non-blocking, event-driven architecture, making it highly efficient for building scalable and real-time applications. Node.js has a vibrant ecosystem of packages and libraries, and it is commonly used for building web servers, APIs, microservices, and various types of networked applications.

In this tutorial, we will not cover an advanced scenario but we will deploy just an Hello World NodeJs.

Our NodeJS application barely requires a set of files:

The app.js file typically serves as the entry point or main file for your application. Its primary purpose is to set up and configure your Express application by defining routes, middleware, and other settings necessary for your web server to function correctly.

Here is the content of the app.js file:

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var greetingRouter = require('./routes/greeting');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/greeting', greetingRouter);

module.exports = app;

As you can see, the above file contains two mapping rules to the URL “/” and “/greeting”.

You can find the routes Javascript files under the routes folder:

├── app.js
├── bin
│   └── www
├── Dockerfile
├── package.json
├── package-lock.json
├── public
│   ├── index.html
│   └── stylesheets
│       └── style.css
└── routes
    ├── greeting.js
    └── index.js

Besides, this project also includes a packages.json file which servers the purpose to keep track of the project’s dependencies. It lists all the Node.js packages (libraries or modules) that your project depends on, including their version numbers.

Deploying the NodeJS Express application on OpenShift

Next, let’s deploy this example application on OpenShift. This can be done with just a single command with the ‘oc’ client tool:

oc new-app https://github.com/fmarchioni/mastertheboss --name=node-app --context-dir=openshift/nodejs-demo

Then, expose the Service through a Route:

oc expose service/node-app

Finally, as soon as the Pod is in Running State, access the application through its Route:

oc get route
NAME       HOST/PORT                          PATH   SERVICES   PORT       TERMINATION   WILDCARD
node-app   node-app-nodejs.apps-crc.testing          node-app   8080-tcp                 None

Here is the Root Context of our NodeJS application:

nodejs on openshift step-by-step tutorial

On the other hand, here is the /greeting Route for the same NodeJS application:

nodejs on openshift tutorial

Conclusion

In this tutorial, we’ve explored the process of deploying a Node.js application on OpenShift, a powerful and scalable container orchestration platform. OpenShift offers a robust and flexible environment for hosting and managing your Node.js applications, allowing you to take full advantage of containerization, auto-scaling, and other modern DevOps practices.

Found the article helpful? if so please follow us on Socials