![]() |
VOOZH | about |
The CData API Server, when paired with the REST connector (or any of over 300 supported data sources), exposes REST 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 REST data in Node.js.
If you haven't already, download and install the CData API Server. After installation, run the application, download the REST connector from within the API Server, and configure your REST 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 REST data using AngularJS, we start by creating and configuring a REST connection. Follow the steps below to configure the API Server to connect to REST data:
Next, create a user to access your REST 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 REST tables:
Having configured a connection to REST 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 REST 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 REST 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 REST 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 REST 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 REST entity, using the following command:
node app_table.js
Accessing REST data from Node.js can be greatly simplified by leveraging an OData API. CData API Server exposes REST data as an OData service, enabling you to perform "Read" operations using standard HTTP requests. This allows you to work with your REST 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