Difficulty: intermediate
Estimated Time: 20-30 minutes

What you will learn

In this scenario you will learn more about Node.js, one of the runtimes included in Red Hat OpenShift Application Runtimes.

You will take an existing sample Node.js application and modify it to address microservice concerns, understand its structure, deploy it to OpenShift and exercise the interfaces between Node.js apps, microservices, and OpenShift/Kubernetes.

What is Node.js?

Logo

Node.js is based on the V8 JavaScript engine from Google and allows you to write server-side JavaScript applications. It provides an I/O model based on events and non-blocking operations that enables you to write applications that are both lightweight and efficient. Node.js also provides a large module ecosystem called npm. Check out the Node.js Runtime Guide for further reading on Node.js and RHOAR.

The Node.js runtime enables you to run Node.js applications and services on OpenShift while providing all the advantages and conveniences of the OpenShift platform such as rolling updates, continuous delivery pipelines, service discovery, and canary deployments. OpenShift also makes it easier for your applications to implement common microservice patterns such as externalized configuration, health check, circuit breaker, and failover.

In this scenario you learned a bit more about what Node.js is, and how it can be used to create modern Javascript microservice-oriented applications.

You took an existing application and modified it to externalize its configuration and separate it from the application itself, and deployed the application to OpenShift and tested it.

Node.js is one of many components of Red Hat OpenShift Application Runtimes - check out other scenarios (for example the Vert.x scenario) to learn about other runtimes you can use.

Additional Resources

More background and related information on ConfigMap can be found here:

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Getting Started with Node.js on OpenShift

Step 1 of 8

Examine the sample project

The sample project shows the components of a Node.js project using Node.js and NPM best practices.

The app implements a simple RESTful microservice which implements a greeting service (that simply returns a Hello greeting).

1. Inspect the application code

Click the links below to open each file and inspect its contents:

  • package.json - Metadata about the project: name, version, dependencies, and other information needed to build and maintain the project.
  • app.js - Main logic of the sample application defining REST endpoints and application runtime configuration.
  • app-config.yml - Content used to initially populate the OpenShift ConfigMap, which the sample app will access at runtime.
  • public/index.html - Simple web UI to access the greeting service.

Review the content a bit and notice that there are some TODO comments in the code. Do not remove them! The comments are used as marker and without them you will not be able finish the scenario.

The /api/greeting API defined in app.js returns a simple message including an optional name. You will modify this file later on.

2. Install Dependencies

Dependencies are listed in the package.json file and declare which external projects this sample app requires. To download and install them, run the following command:

npm install

You can click on the above command (and all others in this scenario) to automatically copy it into the terminal and execute it

It will take a few seconds to download, and you should see a final report such as added 774 packages in 9.79s.

3. Run the application

Before we add code to the project you should build and test that current application starts as it should.

Since this is a working application, run the application locally using npm:

npm start

At this stage the application doesn't really do anything but after a while you will see:

> [email protected] start /root/projects/rhoar-getting-started/nodejs
> node .

3. Test the application

To begin, click on the Local Web Browser tab in the console frame of this browser window. This will open the another tab or window of your browser pointing to port 8080 on your client.

Local Web Browser Tab

You should now see a html page that looks like this

App

This indicates that the app started up correctly. Type in your name into the Name field and click Invoke. The default hard-coded greeting is returned.

Hardcode

4. Stop the application

Before moving on, click in the terminal window and then press CTRL-C to stop the running application!

Congratulations

You have now successfully executed the first step in this scenario.

Now you've seen how you with a few lines of code one can create a simple RESTful HTTP Server capable of serving static content using Node.js.

In next step of this scenario we will deploy our application to OpenShift Container Platform.

Terminal
Local Web Browser
OpenShift Console
Sample Application