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
-
npminstalled.
Procedure
Create the application folder.
$ mkdir myApp
Initialize your application with npm.
The rest of this example assumes the entry point is
app.js, which you are prompted to set when runningnpm init.$ cd myApp $ npm init
Create the entry point in a new file called
app.js.Example
app.jsconst 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'); });Start your application.
$ node app.js Server running at http://localhost:8080
Using
curlor your browser, verify your application is running athttp://localhost:8080.$ curl http://localhost:8080 {"content":"Hello, World!"}
5.2.2. Deploying an application to OpenShift
Prerequisites
-
The
ocCLI client installed. -
npminstalled.
Procedure
Add
nodeshiftto your application.$ npm install nodeshift --save
Add the
openshiftandstartentries to thescriptssection inpackage.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
openshiftscript usesnodeshiftto deploy the application to OpenShift.Optional: Add a
filessection inpackage.json.{ "name": "myApp", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { ... }, "files": [ "package.json", "app.js" ] ... }The
filessection tellsnodeshiftwhat files and directories to include when deploying to OpenShift.nodeshiftuses thenode-tarmodule to create a tar file based on the files and directories you list in thefilessection. This tar file is used whennodeshiftdeploys your application to OpenShift. If thefilessection is not specified,nodeshiftwill send the entire current directory, excluding:-
node_modules/ -
.git/ tmp/It is recommended that you include a
filessection inpackage.jsonto avoid including unnecessary files when deploying to OpenShift.
-
Log in to your OpenShift instance with the
occlient.$ oc login ...
Use
nodeshiftto deploy the application to OpenShift.$ npm run openshift
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
filessection ofpackage.jsonlists all files and directories you need to deploy with your application. -
Ensure the
startentry in thescriptssection ofpackage.jsoncorrectly starts your application. - Ensure all the ports used by your application are correctly exposed when configuring your routes.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.