Ce contenu n'est pas disponible dans la langue sélectionnée.
Chapter 2. Developing and deploying a Node.js application
You can create new Node.js applications and deploy them to OpenShift.
2.1. Developing a Node.js application
For a basic Node.js application, you must create a JavaScript file containing Node.js methods.
Prerequisites
-
npm
installed.
Procedure
Create a new directory
myApp
, and navigate to it.$ mkdir myApp $ cd MyApp
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 runningnpm init
.$ cd myApp $ npm init
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'); });
Start your application.
$ node app.js Server running at http://localhost:8080
Using
curl
or your browser, verify your application is running athttp://localhost:8080
.$ curl http://localhost:8080 {"content":"Hello, World!"}
Additional information
- The Node.js runtime provides the core Node.js API which is documented in the Node.js API documentation.
2.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
.
2.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
nodeshift
to the application. -
Add
openshift
andstart
entries to thepackage.json
file.
Prerequisites
-
npm
installed.
Procedure
Add
nodeshift
to your application.$ npm install nodeshift --save-dev
Add the
openshift
andstart
entries to thescripts
section inpackage.json
.{ "name": "myApp", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "openshift": "nodeshift --expose --dockerImage=registry.access.redhat.com/rhscl/ubi8/nodejs-12", "start": "node app.js", ... } ... }
The
openshift
script usesnodeshift
to deploy the application to OpenShift.NoteUniversal 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
files
section inpackage.json
.{ "name": "myApp", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { ... }, "files": [ "package.json", "app.js" ] ... }
The
files
section tellsnodeshift
what files and directories to include when deploying to OpenShift.nodeshift
uses thenode-tar
module to create a tar file based on the files and directories you list in thefiles
section. This tar file is used whennodeshift
deploys your application to OpenShift. If thefiles
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 inpackage.json
to avoid including unnecessary files when deploying to OpenShift.
-
2.2.2. Deploying a Node.js application to OpenShift
You can deploy a Node.js application to OpenShift using nodeshift
.
Prerequisites
-
The
oc
CLI client installed. -
npm
installed. - Ensure all the ports used by your application are correctly exposed when configuring your routes.
Procedure
Log in to your OpenShift instance with the
oc
client.$ oc login ...
Use
nodeshift
to deploy the application to OpenShift.$ npm run openshift
2.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.json
file of your project, ensure that you install them before running your applications.$ npm install
Deploy the application from the application’s root directory.
$ node app.js Server running at http://localhost:8080
Verification steps
Use
curl
or your browser to verify your application is running athttp://localhost:8080
$ curl http://localhost:8080