Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Zero to Express on OpenShift in Three Commands

April 16, 2018
Lucas Holmquist
Related topics:
Node.js
Related products:
Red Hat OpenShift

Share:

    (Edit: November 22, 2019) The Node images used in this post, both community centos7 and product, are no longer being updated and maintained. For community images, please use the Universal Base Image (UBI)-based node images located here: registry.access.redhat.com/ubi8/nodejs-10

    For a fully supported Product version of Node.js, please check out the Red Hat Software Collections Node.js image, RH SCL Node.js.

    With the recent announcement that Node.js is generally available as part of Red Hat OpenShift Application Runtimes, I wanted to see how easy it was to deploy an Express.js app on OpenShift.

    Getting Started

    Before we start, there are some required prerequisites. You need to have Node 8.x and npm 5.2 or greater installed. npm comes with the official node distribution, so if you install Node from Nodejs.org, you should be good.

    You'll also need access to an OpenShift environment or the Red Hat Container Development Kit (CDK) minishift environment. For this example, I'll be using minishift. You can find instructions on getting minishift up and running here. For my local minishift, I start it with this command:

    $ minishift start --memory=6144 --vm-driver virtualbox

    You also need to be logged in to whatever OpenShift cluster you are using (OpenShift or minishift) using oc login.

    Spoiler Alert

    For those who don't want to read the whole post and don't want to scroll to the end, here are the three commands that need to be run:

    $ npx express-generator .
    $ npx json -I -f package.json -e 'this.scripts.start="PORT=8080 node ./bin/www"'
    $ npx nodeshift --strictSSL=false --expose

    Generate an Express App

    What is Express, you say? Well, according to the Express website, Express is a "Fast, unopinionated, minimalist web framework for Node.js."

    One pretty cool thing about Express is the Express application generator tool: express-generator. This is a command-line tool that "quickly creates an application skeleton". But wait: didn't I just say that Express was unopinionated? It is, but this is the opinionated skeleton creator. ¯_(ツ)_/¯

    The Express website recommends installing the express-generator module globally, like this:

    npm install -g express-generator

    But we aren't going to do that. Instead, we are going to use a fairly new feature from npm, called npx.

    npx gives us the ability to run one-off commands with out having to install things globally. There is more to npx that just that feature, so if you are interested in all the cool things npx can do, check it out here.

    With this new-found knowledge, we can now generate our Express app like this:

    $ npx express-generator .

    Let's take a quick look at what is actually happening with this command. First, npx sees that we want to run the express-generator command, so npx does some magic to see if we have it installed locally (in our current directory), and then it checks our global modules. Because it is not there, it downloads it for this one-time use.

    express-generator is run in our current directory, which is denoted by that . at the end of the command.

    The result should look something like this:

    express-generator also gives us some instructions on how to install the dependencies and then how to run the application. You can skip that for now.

    Update the package.json File

    Now that we created our basic Express application using one command, we need to add one thing to package.json before we deploy our app.

    We need to pass a PORT environment variable to our start script.

    One way to do this is to open a text editor and do it that way, but that would add a few more steps. To do this in one command, we can use the json module.

    $ npx json -I -f package.json -e 'this.scripts.start="PORT=8080 node ./bin/www"'

    As before, we are using the npx command to allow us to not have to install the json module globally.

    Let's see what is going on with the options passed to the json module.

    -I -f package.json means that we want to edit in place the file package.json. The -e option will execute some JavaScript code, which in this case is setting the scripts.start property from package.json with the string "PORT=8080 node ./bin/www".

    For more information on the json module, check out the documentation.

    Deploy the Application to OpenShift

    And now, the final step is to run this command:

    $ npx nodeshift --strictSSL=false --expose

    Here, we are using the nodeshift module to deploy our application. nodeshift is a CLI or programmable API that helps with deploying Node apps to OpenShift.

    npx is doing the same thing as in the previous examples.

    nodeshift is using two flags. The first, strictSSL=false, is needed when deploying to minishift or someplace that is using a self-signed certificate. If we were deploying to a real OpenShift cluster, we could leave that out.

    The second flag, expose, tells nodeshift that it should create a route for us, which allows our application to be seen by the outside world. (If you are running minishift locally, only you can see the application.)

    The output of this command will look something like this:

    If we head over to the web UI of our running minishift, we can see that the created pod is now running successfully.

    Then, if we click the link, we can see our example app running:

    Note: The example above will use the latest community s2i images (9.x at the time of this writing). To use a fully supported version of Node.js on OpenShift all you need is to add the "--dockerImage" flag.

    This will integrate the Red Hat OpenShift Application Runtime version Node.js (8.x) which you can get full production and developer support as part of our product subscription.

    This might look something like this:

    $ npx nodeshift --strictSSL=false --expose --dockerImage=registry.access.redhat.com/rhoar-nodejs/nodejs-8

    Recap

    In this post, the commands were a little spread out, so let's see them all together again:

    $ npx express-generator .
    $ npx json -I -f package.json -e 'this.scripts.start="PORT=8080 node ./bin/www"'
    $ npx nodeshift --strictSSL=false --expose

    The example app we created was very simple, but it shows how quickly you can get started using Node.js on OpenShift.

    Last updated: January 12, 2024

    Recent Posts

    • How Trilio secures OpenShift virtual machines and containers

    • How to implement observability with Node.js and Llama Stack

    • How to encrypt RHEL images for Azure confidential VMs

    • How to manage RHEL virtual machines with Podman Desktop

    • Speech-to-text with Whisper and Red Hat AI Inference Server

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue