There’s a new web engine in town and its name is Node.js. But what the heck is Node.js?? Sources say that,”Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.” Sounds cool. But lets see if this new technology can read from oracle. We’ll lets at-least install it and see…
  1. You’ll need to install an oracle client
  2. Setup your oracle environment
    1. DEVTOOL=/opt/rh/devtoolset-2/root/usr/bin ORACLE_HOME=/opt/oracle/app/oracle/product/11.2.0/client_1 OCI_LIB_DIR=$ORACLE_HOME/lib OCI_INC_DIR=$ORACLE_HOME/rdbms/public CPLUS_INCLUDE_PATH=/opt/oracle/app/oracle/product/11.2.0/client_1/rdbms/public NODE_PATH=/usr/lib/node_modules LD_LIBRARY_PATH=$OCI_LIB_DIRPATH=$DEVTOOL:$PATH:$HOME/bin:$ORACLE_HOME/bin:$OCI_LIB_DIRexport PATH DEVTOOL ORACLE_HOME OCI_LIB_DIR OCI_INC_DIR CPLUS_INCLUDE_PATH NODE_PATH LD_LIBRARY_PATH
  3. Now install Node.js. We elected to use Centos 6.7
    1. yum install epel-release yum install nodejs npm
  4. Now confirm node is installed by issuing node –version
  5. Now install the necessary modules like npm, express, and etc – modules live /usr/lib/node_modules
    1. npm install npm -g npm install nodejs -g npm install express -g
  6. Next install Python and development tools- it allows us to integrate the oracle driver with express.
    1. cd opt wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz ./configure make make altinstall
    2. cd /etc/yum.repos.d vi DevToolset.repo past the below [DevToolset-2] name=RedHat DevToolset v2 $releasever – $basearch baseurl=http://puias.princeton.edu/data/puias/DevToolset/$releasever/$basearch/ enabled=1 gpgcheck=0yum install devtoolset-2-gcc devtoolset-2-gcc-c++
  7. Now install the oracle driver
    1. cd /usr/lib npm install –python=/opt/Python-2.7.11/python oracle -g
Ok. The environment is configured and ready for a test. Lets run a connectivity test for node and oracle
  1. create a test.js and paste the below in the file
    1. var oracle = require(‘oracle’);var connectData = { hostname: “awsrds.kjdlkfajsdklfjasl.ap-northeast-1.rds.amazonaws.com”, port: 1521, database: “DBSID”, // System ID (SID) user: “?????”, password: “???????” }oracle.connect(connectData, function(err, connection) { if (err) { console.log(“Error connecting to db:”, err); return; } connection.execute(“select * from v$database”, [], function(err, results) { if (err) { console.log(“Error executing query:”, err); return; } console.log(results); connection.close(); // call only when query is finished executing }); });
  2. Now run the site by issuing node test.js
You can also the restful api route.
  1. open a restful.js and paste the below in the file
    1. var express = require(‘express’); var app = express(); // This make a uri end point for the REST call // http://myhost/emp will return the data app.get(‘/emp’, function(req, res){ empList(req,res) } );var oracle = require(‘oracle’); var conn;var connectData = { hostname: “awsrds.kjdlkfajsdklfjasl.ap-northeast-1.rds.amazonaws.com”, port: 1521, database: “DBSID”, // System ID (SID) user: “?????”, password: “???????” } // connect //var conn=””; oracle.connect(connectData, function(err, connection) { if (err) { console.log(“Error connecting to db:”, err); return; } console.log(‘connected’); conn =connection; }); // function registered in the /emp declaration function empList(req,res){ var empList = “select * from loynav_0450_pmj.ln_environments”; conn.execute(empList, [], function(err, results) { if (err) { console.log(“Error executing query:”, err); return; } // use the JSON function to format the results and print res.write(JSON.stringify(results)); res.end(); }); }