![]() |
VOOZH | about |
The CData API Server, when paired with the Azure Data Catalog connector (or any of over 300 supported data sources), exposes Azure Data Catalog data as an OData web service that can be queried from Node.js using simple HTTP requests. This article walks you through setting up the CData API Server and demonstrates the process to retrieve JSON-formatted Azure Data Catalog data in Node.js.
If you haven't already, download and install the CData API Server. After installation, run the application, download the Azure Data Catalog connector from within the API Server, and configure your Azure Data Catalog data connection. Then, configure API Server to generate OData feeds for any tables you want to access in your single-page application (SPA).
AngularJS requires servers to have CORS (Cross-Origin Resource Sharing) enabled. To enable CORS, go to the Server tab under the Settings section in the API Server and adjust the following settings:
To work with Azure Data Catalog data using AngularJS, we start by creating and configuring a Azure Data Catalog connection. Follow the steps below to configure the API Server to connect to Azure Data Catalog data:
Next, create a user to access your Azure Data Catalog data through the API Server. You can add and configure users on the Users page. Follow the steps below to configure and create a user:
Having created a user, you are ready to create API endpoints for the Azure Data Catalog tables:
Having configured a connection to Azure Data Catalog data, created a user, and added resources to the API Server, you now have an easily accessible REST API based on the OData protocol for those resources. From the API page in API Server, you can view and copy the API Endpoints for the API:
π API EndpointsJust like with standard OData feeds, you can limit the fields returned by adding a $select parameter to the query. You can also include other standard OData URL parameters, such as $filter, $orderby, $skip, and $top. Refer to the help documentation for more details on supported OData queries.
Select the user you created, then copy and save the authtoken for future use. You can also toggle on 'Token Expiration' and set the number of days based on your use case. Click Save to save the details.
π Copy and save authtokenOData feeds provide a standardized way to access data and can be easily consumed in Node.js. Node.js scripts use the built-in http module to make requests to the CData API Server, which acts as an intermediary to expose Azure Data Catalog data as OData. The process involves constructing HTTP GET requests to the API Server's OData endpoints, handling the responses, and parsing the JSON data.
To get started, we'll create a new Node.js project and install the necessary packages to run it.
cd D:\ mkdir my-odata-app cd my-odata-app npm init -y
npm install http
In this section, we'll work on retrieving specific records from a Azure Data Catalog entity by filtering based on a unique identifier using the app_data.js script.
var http = require('http');
// Function to fetch data based on a given Id
function fetchDataById(id) {
http.get({
protocol: "http:",
hostname: "localhost",
port: 8080,
path: "/api.rsc/DEMO_DB_CRM_Account?$filter=" + encodeURIComponent("Id eq '" + id + "'"), // Updated URL with filter
auth: 'your_username:your_authtoken' // Enter your username and authtoken/password
}, function (res) {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
var jsonData = JSON.parse(body);
console.log(jsonData);
});
}).on('error', function (e) {
console.log("Error: ", e);
});
}
// Example usage: Fetch data for Id '11'
fetchDataById('11');
Retrieve the specific records for an unique identifier from the Azure Data Catalog entity, using the following command in the Terminal or Command Prompt:
node app_data.js
The app_table.js script focuses on fetching all records from a specified Azure Data Catalog entity.
var http = require('http');
http.get({
protocol: "http:",
hostname: "localhost",
port: 8080,
path: "/api.rsc/DEMO_DB_CRM_Account", // Updated URL to fetch data from DEMO_DB_CRM_Account
auth: 'your_username:your_password' // Enter your username and password/authtoken
}, function (res) {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
console.log(body);
var jsonData = JSON.parse(body);
console.log(jsonData);
});
}).on('error', function (e) {
console.log("Error: ", e);
});
Retrieve all the records from the Azure Data Catalog entity, using the following command:
node app_table.js
Accessing Azure Data Catalog data from Node.js can be greatly simplified by leveraging an OData API. CData API Server exposes Azure Data Catalog data as an OData service, enabling you to perform "Read" operations using standard HTTP requests. This allows you to work with your Azure Data Catalog data in a more structured and standardized way.
To see how API Server can simplify your data access, download a free, 30-day trial of CData API Server today! For more general information on our API Server and to see what other data sources we support, refer to our API Server page.Learn more or sign up for a free trial:
CData API Server