Chapter 5. Developing an application for the Node.js runtime

5.1. Node.js runtime API

The Node.js runtime provides the core Node.js API which is documented in the Node.js API documentation.

5.2. Creating a Node.js application

In addition to using a booster, you can also create new Node.js applications from scratch and deploy them to OpenShift.

5.2.1. Creating an application

Prerequisites

  • npm installed.

Procedure

  1. Create the application folder.

    $ mkdir myApp
  2. Initialize your application with npm.

    The rest of this example assumes the entry point is app.js, which you are prompted to set when running npm init.

    $ cd myApp
    $ npm init
  3. Create the entry point in a new file called app.js.

    Example app.js

    const http = require('http');
    
    const server = http.createServer((request, response) => {
      response.statusCode = 200;
      response.setHeader('Content-Type', 'application/json');
    
      const greeting = {content: 'Hello, World!'};
    
      response.write(JSON.stringify(greeting));
      response.end();
    });
    
    server.listen(8080, () => {
      console.log('Server running at http://localhost:8080');
    });

  4. Start your application.

    $ node app.js
    Server running at http://localhost:8080
  5. Using curl or your browser, verify your application is running at http://localhost:8080.

    $ curl http://localhost:8080
    {"content":"Hello, World!"}

5.2.2. Deploying an application to OpenShift

Prerequisites

  • The oc CLI client installed.
  • npm installed.

Procedure

  1. Add nodeshift to your application.

    $ npm install nodeshift --save
  2. Add the openshift and start entries to the scripts section in package.json.

    {
      "name": "myApp",
      "version": "1.0.0",
      "description": "",
      "main": "app.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "openshift": "nodeshift --strictSSL=false --metadata.out=deployment-metadata.json --build.forcePull=true --dockerImage=registry.access.redhat.com/rhoar-nodejs/nodejs-8",
        "start": "node app.js",
        ...
      }
      ...
    }

    The openshift script uses nodeshift to deploy the application to OpenShift.

  3. Optional: Add a files section in package.json.

    {
      "name": "myApp",
      "version": "1.0.0",
      "description": "",
      "main": "app.js",
      "scripts": {
        ...
      },
      "files": [
        "package.json",
        "app.js"
      ]
      ...
    }

    The files section tells nodeshift what files and directories to include when deploying to OpenShift. nodeshift uses the node-tar module to create a tar file based on the files and directories you list in the files section. This tar file is used when nodeshift deploys your application to OpenShift. If the files section is not specified, nodeshift will send the entire current directory, excluding:

    • node_modules/
    • .git/
    • tmp/

      It is recommended that you include a files section in package.json to avoid including unnecessary files when deploying to OpenShift.

  4. Log in to your OpenShift instance with the oc client.

    $ oc login ...
  5. Use nodeshift to deploy the application to OpenShift.

    $ npm run openshift
  6. Expose the application in OpenShift using a route.

    Example for exposing a service using a route

    $ oc expose service myapp

Optionally, you can create a .nodeshift directory at the root of your project to include deployment yaml files. These files will create items such as routes when deploying your application to OpenShift using nodeshift. For example, to add a route during deployment, you could create a .nodeshift/route.yaml with the following:

Example .nodeshift/route.yaml

apiVersion: v1
kind: Route
metadata:
  name: myapp
spec:
  port:
    targetPort: 8080
  to:
    kind: Service
    name: myapp

5.3. Deploying an existing Node.js application to OpenShift

You can deploy an existing Node.js application to OpenShift using the steps in Section 5.2.2, “Deploying an application to OpenShift”.

In addition, you must verify the following items when deploying an existing application:

  • Ensure the files section of package.json lists all files and directories you need to deploy with your application.
  • Ensure the start entry in the scripts section of package.json correctly starts your application.
  • Ensure all the ports used by your application are correctly exposed when configuring your routes.