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:

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