mercoledì 30 marzo 2016

Oracle Application Container Cloud & Developer Cloud Service: How to create a Node.js application with DB connection in pool (in cloud) and how to deploy it on Oracle Application Container Cloud (Node.js) using Developer Cloud Service

In this post I'll explain:
  • How to create (without an IDE) a local Node.js Application with DB connection (in Cloud) using the oracledb driver for node.js
  • How to package and load the Application on Oracle Application Container Cloud (Node.js Container)
  • How to create and test the same application with Eclipse 
  • How to push the Application on Oracle Developer Cloud Service (DevOps in Cloud)
  • How to use Developer Cloud Service to be able to automate the packaging & deploy operations on Oracle Application Container Cloud (Node.js Container)
You can DOWNLOAD from HERE the entire project (with source code) described in this post:

The Prerequisites:

For this guide you need to install on your local PC:
I suggest to follow this step by step guide for the installation of oracledb driver:
  • I have succesfully tested the previous link on Windows 7. 
  • On Windows 10 I had a problem with Visual Studio executing the command "npm install oracledb" but I have solved it setting: 
    • set VCTargetsPath=C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120
This was the problem (the red part):

Once you have installed Node.js & oracledb driver, we can download the application from the examples page of oracledb:

From the Examples directory we need to get:
  • webapp.js - main project, with oracledb and pool connections
  • dbconfig.js - properties for oracledb connection
We will deploy the application on Oracle Application Container Cloud so we need to change two lines of code in webapp.js (to be able to test it both locally and remotely):
  • Change "var portid = 7000;" in "var portid = process.env.PORT || 80;"
  • Change "hs.listen(portid, "localhost");" in "hs.listen(portid);"
The application is using the following db schema:
You can download the schema with all the data from [HERE]

If you are using an oracle db you can find it under user HR.

I have used a DB in Cloud (Oracle Database Cloud Service):

Those are the required parameters to connect to the table Employee under user HR: (sqldeveloper image)
As described in this post, remember to select "Oracle Compute Cloud Service" then select "Network", here you must enable the seurity rules ora_p2_dblistener:

We must insert those parametrs in the file dbconfig.js:

 module.exports = {  
  user     : process.env.NODE_ORACLEDB_USER || "hr",  
  // Instead of hard coding the password, consider prompting for it,  
  // passing it in an environment variable via process.env, or using  
  // External Authentication.  
  password   : process.env.NODE_ORACLEDB_PASSWORD || "hr_pwd",  
  // For information on connection strings see:  
  connectString : process.env.NODE_ORACLEDB_CONNECTIONSTRING || "",  
  // Setting externalAuth is optional. It defaults to false. See:  
  externalAuth : process.env.NODE_ORACLEDB_EXTERNALAUTH ? true : false  

Put the webapp.js and dbconfig.js files (both modified as previous described) in a directory, open a command console and test your application in local:

We will talk about the manifest.json later.

Open a Command Console, go to the directory and execute the command "node webapp.js":

Open a Browser and test your application, the number after the "/" is required for the WHERE clause query in the webapp.js code "SELECT employee_id, first_name, last_name FROM employees WHERE  department_id = :id"  (try different numbers 10, 20, 30 ...100 ...)

It is time now to package our application in a zip file and to load it, manually, in Oracle Application Container Cloud - Node.js .

You mast create now a file named manifest.json with inside the command needed to start our application "node webapp.js".

This is our manifest.json file:

  "command": "node webapp.js",  
  "release": {},  
  "notes": ""  

Now we can create a zip file with inside: manifest.json, webapp.js and dbconfig.js:

The zip in our directory:

The content of our zip file:

Connect now to your Oracle Application Container Cloud and load your Application:
Click on Create Application and choose "Node":

Fille the required items and upload your zip file:
Select "Upload application archive" and click on the button "Choose File":

Select your file on your local disk and click open:

Click on Create:

After a while you will get this result:

Wait for the creation of the application:

Click on the generated URL to test your Application:

This is your application up and working:

We have created, packaged and deployed manually the application, now we will see how to use Eclipse and Developer Cloud Service to automate all the tasks of development/test,  packaging and deploy on Oracle Cloud:

I have installed on Eclipse the "nodeclipse plugin", to do this start a new project on Eclipse (OEPE: Oracle Enterprise Pack for Eclipse ( then select Help->Eclipse Marketplace ...

Search for nodeclipse and install it:

To create a new Node project select: File->New->Other ...
Select Node->Node.js Project:

Choose a name for your project (DemoNode) and click on Finish:

This is your new project:

Drag & Drop dbconfig.js, webapp.js, manifest.json on the root of your project:

To test your application:
right click on webapp.js and select Run As->Run Configurations ...

We need to add an environment variable with the path of the node.js modules:

This is your environment variable:
Click Apply
Click Run

As you see in the console tab, your application is now up and running, on local pc, and you can test it with a browser:

Go to your DevCS domain and follow this previous post to create a new project with a Git repository:

From Eclpse configure your Oracle Cloud Plugin connect it to your DevCs Domain, drag & Drop your project from Eclipse in the Oracle Cloud Plugin as described here:

This is my DevCS, with an empty GIT repository:

This is my Eclipse project with the Oracle Cloud Plugin:
I have added in the project the following file [] :
IMPORTANT: Be sure that is saved with EOL in UNIX mode. (in notepad++ you can do this from Edit->EOL Conversion->UNIX/OSX Format)

 export ID_DOMAIN=$1  
 export USER_ID=$2  
 export USER_PASSWORD=$3  
 export APP_NAME=$4  
 export ARCHIVE_FILE=$5  
 zip -r .  
 echo '\n[info] Creating container\n'  
 curl -i -X PUT \  
   -u ${USER_ID}:${USER_PASSWORD} \  
 echo '\n[info] Uploading application to storage\n'  
 curl -i -X PUT \  
  -u ${USER_ID}:${USER_PASSWORD} \  
 # See if application exists  
 let httpCode=`curl -i -X GET \  
  -u ${USER_ID}:${USER_PASSWORD} \  
  -H "Content-Type: multipart/form-data" \  
  -sL -w "%{http_code}" \  
  https://${APAAS_HOST}/paas/service/apaas/api/v1.1/apps/${ID_DOMAIN}/${APP_NAME} \  
  -o /dev/null`  
 # If application exists...  
 if [ $httpCode == 200 ]  
  # Update application  
  echo '\n[info] Updating application...\n'  
  curl -i -X PUT \  
   -u ${USER_ID}:${USER_PASSWORD} \  
   -H "Content-Type: multipart/form-data" \  
   -F archiveURL=${APP_NAME}/${ARCHIVE_FILE} \  
  # Create application and deploy  
  echo '\n[info] Creating application...\n'  
  curl -i -X POST \  
   -u ${USER_ID}:${USER_PASSWORD} \  
   -H "Content-Type: multipart/form-data" \  
   -F "name=${APP_NAME}" \  
   -F "runtime=node" \  
   -F "subscription=Hourly" \  
   -F archiveURL=${APP_NAME}/${ARCHIVE_FILE} \  
 echo '\n[info] Deployment complete\n'  

In red I have marked some important part:

In you are not in Europe you need to change this

Here we are creating the zip file with the required manifest.json:
zip -r .

We are deploying in Node.js Container so we need:
-F "runtime=node" \

If you would like to use the same script in JavaSE Container change it in
-F "runtime=java" \

I have dragged and dropped DemoNode project on Demo_Node.js folder on the Cloud Plugin:
In the Synchronize window deselect "Generate or update Maven ..." and "Create Hudson build job", we will package and deploy the application with script that uses Rest API for Oracle Cloud, in this way (no Maven) you can learn a different way to use DevCS.

This is our local project synchronized with the Git on DevCS:

On the Code tab of DevCs you can see now your project:

We can create a new Build Job, select the Build tab and click on the green button New Job:

Choose a name for the build job:
Click Save:

In Source Control tab of you buil job select your Git repository:

In the Build Steps, click on Add Build Step and select Execute Shell:
Insert the following commands:

 cd DemoNode  
 /bin/sh -xe gse00000410 cloud.admin mypassword provaNodeJs  

With this command you go inside the directory DemoNode, here there are all the files of your project, there is also the

You must pass some parameters to
The Identity Domain : gse00000410
User: cloud.admin
Password : mypassword
Name of the project we want create on Oracle Application Container Cloud =  provaNodeJs
Zip file to upload:

Click the Save Button:

Click on Build Now button:

You can see a progress bar:

If everything goes well, you will get green icon:

Go to your Oracle Application Container Cloud and you will see the new project: (provaNodeJs)

Click on the generated URL to test it:

1 commento: