Oracle by Example brandingConnect a Node.js Application to Oracle Database Exadata Express Cloud Service

section 0Before You Begin

This 15-minute tutorial shows you how to connect a Node.js application to Oracle Database Exadata Express Cloud Service using Oracle Application Container Cloud Service.

Background

Oracle Application Container Cloud Service lets you deploy Java SE, Node.js, PHP, and Python applications to the Oracle Cloud.

Oracle Database Exadata Express Cloud Service delivers a managed Oracle Database 12c Release 2 Enterprise Edition with options, running on Oracle Exadata engineered systems – all at a highly affordable entry-level price. It is a full Oracle Database experience, compatible with what you get on-premises or in other Oracle Database Cloud Services, provisioned within minutes, and suitable for small to medium sized data.

This tutorial shows you how integrate both services using a Node.js application.

What Do You Need?

  • Access to an instance of Oracle Application Container Cloud Service (Make a note of your account information: identity domain, user name, and password.)
  • An Oracle Database Exadata Express Cloud Service account

section 1Create the Node.js Application

  1. Create a directory named connection-test-app in your local system.
  2. In the connection-test-app directory, create the manifest.json file and add the following content:
    {
      "runtime":{
        "majorVersion":"6"
      },
      "command": "node index.js",
      "release": {},
      "notes": ""
    } 
  3. In the same directory, create the index.js file with the following content:
    const http = require('http');
    const oracledb = require('oracledb');
    let error;
    let user;
     
    oracledb.getConnection(
        {
          user: process.env.EECS_USER, 
          password: process.env.EECS_PASSWORD,
          connectString: 'dbaccess'
        }, 
        function(err, connection) {
          if (err) {error = err; return;}
          
          connection.execute('select user from dual', [], function(err, result) {
            if (err) {error = err; return;} 
            user = result.rows[0][0];
            error = null; 
            connection.close(function(err) {
              if (err) {console.log(err);}
            });
          })
        }
    );
     
    http.createServer(function(request, response) {
      response.writeHead(200, {'Content-Type': 'text/plain'});
     
      if (error === null) {
        response.end('Connection test succeeded. You connected to Exadata Express as ' + user + '!');
      } else if (error instanceof Error) {
        response.write('Connection test failed. Check the settings and redeploy app!\n');
        response.end(error.message);
      } else {
        response.end('Connection test pending. Refresh after a few seconds...');
      }
    }).listen(process.env.PORT);
                                 

section 2Obtain the Oracle Database Exadata Express Cloud Service Client Credentials

  1. Login into Oracle Database Exadata Express Cloud Service.
  2. Navigate to the Service Console for Exadata Express and open the service console.
  3. Click Develop.
  4. If Client Access is not currently enabled, click Client Access.
    Enable client access
    Description of the illustration credentials_01.png
  5. In the Client Credentials dialog box, click Enable Client Access.
  6. On the Develop page, click Client Credentials.
  7. In the Client Credentials dialog box, enter Password, Confirm Password, and click Download.
    Download client credentials
    Description of the illustration credentials_02.png
  8. Save the client wallet client_credentials.zip file.
  9. Extract the content of the client_credentials.zip file to the connection-test-app directory.
  10. Open the connection-test-app/sqlnet.ora file in a text editor and change the value of DIRECTORY from ?/network/admin to /u01/app/client_credentials
  11. Compress the contents of the connection-test-app directory in a zip file named connection-test-app.zip.

section 3Deploy the Application to Oracle Application Container Cloud Service

  1. Open the Oracle Application Container Cloud Service console.
  2. In the Applications list view, click Create Application.
  3. In the Create Application dialog box, click Node.
  4. In the Application section, enter TestNodeEecs for the name of your application. Click Browse.
  5. In the File Upload dialog box, select the connection-test-app.zip file, and click Open.
  6. Keep the default values in the Instances and Memory fields and click Create.

section 4Add the Environment Variables

In this section, you add three environment variables: TNS_ADMIN, EECS_USER, and EECS_PASSWORD.

  1. In the Applications list view, click the TestNodeEecs application name.
  2. Select the Deployments tab.
  3. Click Add under Environment Variables.
  4. In the Add Environment Variable diaglog box, enter the following values, and click Save:
    • Name: TNS_ADMIN
    • Value: $APP_HOME/client_credentials
  5. Repite the steps 3 and 4 to add the EECS_USER and EECS_PASSWORD environment variables.

    Note: This tutorial uses the PDB_ADMIN user for speed and simplicity, but it's not recommended in a production environment. You should create another user in your Oracle Database Exadata Express Cloud Service instance.

    Name Value
    EECS_USER PDB_ADMIN
    EECS_PASSWORD <Enter the password of the PDB_ADMIN user>
    Environment variables
    Description of the illustration environment_variables_01.png
  6. In the Deployments dialog box, click Apply Edits.

section 5Test Your Application

Wait until the application is completly restarted and then click the URL.

Firefox window - TestNodeApplication response
Description of the illustration test_app_01.png

more informationWant to Learn More?