Build REST API using Oracle Cloud ADB

Yogesh Abhyankar
3 min readJul 30, 2022

--

You might have seen lot of articles with node webserver and some with node REST API. Here I am demonstrating a webserver which is consuming a REST API created in oracle OCI.
So what components to be used and in what order is what you will see shortly.
In this article, I will first show you how to build a REST API and then use it in a webserver to create a useful tool. I will add some dynamic functionality so the tool will be generic.
The goal will be to create an API that selects rows from a table and then using that data do something useful.
First section will cover database basics (using node oracledb).
Before starting, here are prerequisites that you need
Access to oracle database — here I am using oracle cloud autonomous database but you can use any oracle database
node and npm and git to clone repository
For this example, I am using my oracle oci cloud shell to create restapi and webserver but you can use your local pc where you have node installed

at command prompt create a new folder
mkdir dbdemo
cd dbdemo
create another folder for restapi
mkdir node-restapi
cd node-restapi
Create a new node.js project
npm init -y
Install express and oracledb libraries with npm:
npm i express
npm i oracledb

express is for web server and oracledb to connect with oracle database

Create a file with name index.js and paste the following code
const express = require(‘express’)
const app = express();
const port = 5000

const oracledb = require(‘oracledb’);
var sql = ‘select * from xx’;

async function selectTable(req, res, tab) {
try {
sql = ‘select * from xx’;
sql = sql.replace(“xx”,tab);

connection = await oracledb.getConnection({
user: user,
password: password,
connectString: connstr
});

// run query to get all records
result = await connection.execute(sql,[], {});

} finally {
if (connection) {
try {
// Always close connections
await connection.close();
} catch (err) {
console.error(err.message);
} }
return res.send(result.rows);
}
}

app.get(‘/table’, function (req, res) {
let tab = req.query.tab;
selectTable(req, res, tab);
})

Get table data
The rest api will send data from table. The API is generic so as to get data from any table that you wish to select

//GET request to fetch
run node index.js and go to browser and type
http://localhost:5000/table?tab=all_views

JSON data returned by API

This will fetch data of all_views table, you can type any table name, just replace “all_views” with any other table name
You can download the sample code from github here
you can either clone repository or download zip file. Once you download type node install
replace the db user password with actual values in index.js and
run node index.js and you are good to go

This concludes the first part, in next part we will create webserver and call this api to display table data. Dynamic HTML to display Oracle Table

--

--

Yogesh Abhyankar

A passionate IT consultant, focused on shifts in technological evolution. Enthusiast to learn Machine Learning, Chatbot and related stuff