このコンテンツは選択した言語では利用できません。

Chapter 6. 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.

6.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

  1. Create a new directory myApp, and navigate to it.

    $ mkdir myApp
    $ cd MyApp
    Copy to Clipboard Toggle word wrap

    This is the root directory for the application.

  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
    Copy to Clipboard Toggle word wrap
  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');
    });
    Copy to Clipboard Toggle word wrap

  4. Start your application.

    $ node app.js
    Server running at http://localhost:8080
    Copy to Clipboard Toggle word wrap
  5. Using curl or your browser, verify your application is running at http://localhost:8080.

    $ curl http://localhost:8080
    {"content":"Hello, World!"}
    Copy to Clipboard Toggle word wrap

Additional information

6.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.

6.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 and start entries to the package.json file.

Prerequisites

  • npm installed.

Procedure

  1. Add nodeshift to your application.

    $ npm install nodeshift --save-dev
    Copy to Clipboard Toggle word wrap
  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 --expose --dockerImage=registry.access.redhat.com/rhscl/ubi8/nodejs-10",
        "start": "node app.js",
        ...
      }
      ...
    }
    Copy to Clipboard Toggle word wrap

    The openshift script uses nodeshift to 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.

  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"
      ]
      ...
    }
    Copy to Clipboard Toggle word wrap

    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.

6.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

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

    $ oc login ...
    Copy to Clipboard Toggle word wrap
  2. Use nodeshift to deploy the application to OpenShift.

    $ npm run openshift
    Copy to Clipboard Toggle word wrap

6.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.4.1 installed
  • RHEL 7 or RHEL 8 installed.
  • Node.js installed

Procedure

  1. 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
    Copy to Clipboard Toggle word wrap
  2. Deploy the application from the application’s root directory.

    $ node app.js
    Server running at http://localhost:8080
    Copy to Clipboard Toggle word wrap

Verification steps

  1. Use curl or your browser to verify your application is running at http://localhost:8080

    $ curl http://localhost:8080
    Copy to Clipboard Toggle word wrap
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat