Chapter 3. Developing and deploying a Node.js application
In addition to using an example, you can create new Node.js applications from scratch and deploy them to OpenShift.
3.1. Developing a Node.js application
For a basic Node.js application, you must create a JavaScript file containing Node.js methods.
Prerequisites
- 
						npminstalled.
Procedure
- Create a new directory - myApp, and navigate to it.- mkdir myApp cd MyApp - $ mkdir myApp $ cd MyApp- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - This is the root directory for the application. 
- 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 - $ cd myApp $ npm init- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Create the entry point in a new file called - app.js.- Example - app.js- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Start your application. - node app.js - $ node app.js Server running at http://localhost:8080- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Using - curlor your browser, verify your application is running at- http://localhost:8080.- curl http://localhost:8080 - $ curl http://localhost:8080 {"content":"Hello, World!"}- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
Additional information
- The Node.js runtime provides the core Node.js API which is documented in the Node.js API documentation.
3.2. Deploying a Node.js application to Openshift
				To deploy your Node.js application to OpenShift, add nodeshift to the application, configure the package.json file and then deploy using nodeshift.
			
3.2.1. Preparing Node.js application for OpenShift deployment
To prepare a Node.js application for OpenShift deployment, you must perform the following steps:
- 
							Add nodeshiftto the application.
- 
							Add openshiftandstartentries to thepackage.jsonfile.
Prerequisites
- 
							npminstalled.
Procedure
- Add - nodeshiftto your application.- npm install nodeshift --save-dev - $ npm install nodeshift --save-dev- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Add the - openshiftand- startentries to the- scriptssection in- package.json.- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - The - openshiftscript uses- nodeshiftto deploy the application to OpenShift.Note- Universal base images and RHEL images are available for Node.js. See the Node.js release notes for more information on image names. 
- Optional: Add a - filessection in- package.json.- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - The - filessection tells- nodeshiftwhat files and directories to include when deploying to OpenShift.- nodeshiftuses the- node-tarmodule to create a tar file based on the files and directories you list in the- filessection. This tar file is used when- nodeshiftdeploys your application to OpenShift. If the- filessection is not specified,- nodeshiftwill send the entire current directory, excluding:- 
									node_modules/
- 
									.git/
- tmp/- It is recommended that you include a - filessection in- package.jsonto avoid including unnecessary files when deploying to OpenShift.
 
- 
									
3.2.2. Deploying a Node.js application to OpenShift
					You can deploy a Node.js application to OpenShift using nodeshift.
				
Prerequisites
- 
							The ocCLI client installed.
- 
							npminstalled.
- Ensure all the ports used by your application are correctly exposed when configuring your routes.
Procedure
- Log in to your OpenShift instance with the - occlient.- oc login ... - $ oc login ...- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Use - nodeshiftto deploy the application to OpenShift.- npm run openshift - $ npm run openshift- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
3.3. Deploying a Node.js application to stand-alone Red Hat Enterprise Linux
				You can deploy a Node.js application to stand-alone Red Hat Enterprise Linux using npm.
			
Prerequisites
- A Node.js application.
- npm 6.14.8 installed
- RHEL 7 or RHEL 8 installed.
- Node.js installed
Procedure
- If you have specified additional dependencies in the - package.jsonfile of your project, ensure that you install them before running your applications.- npm install - $ npm install- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Deploy the application from the application’s root directory. - node app.js - $ node app.js Server running at http://localhost:8080- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
Verification steps
- Use - curlor your browser to verify your application is running at- http://localhost:8080- curl http://localhost:8080 - $ curl http://localhost:8080- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow