question

Upvotes
Accepted
76 16 21 29

Server-Side Data Fusion API Call with Node.js

I am able to run this code fine from a client-side app:

    $.ajaxSetup({
        beforeSend: function(xhr, settings) {
            xhr.setRequestHeader('Authorization', "Bearer " + myAccessToken);
        }
    });
    $.ajax(myURL, {
        dataType: 'json',
        type: 'get',
        success: function(data, status) {
            dataReturned1(data);
        },
        error: function(xhr, status, error) {
            console.log(xhr);
        }
    });

The problem comes when I attempt to run this server-side using Node.js, something like this:

var express = require('express');
var app = express();
var fs = require("fs");
var $ = require("jquery");
app.get('/sellsidebuyside', function (req, res) {
    // AJAX REQUEST AS ABOVE HERE
})
var server = app.listen(8081, function () {
   var host = server.address().address
   var port = server.address().port
   console.log("Example app listening at http://%s:%s", host, port)
})

The error message is:

TypeError: $.ajaxSetup is not a function

From online forums I gather that jQuery was perhaps never designed to run server-side, so this could be the root of my issue. It is suggested to use a standard XMLHttpRequest, but I'm not sure how to configure this to make a cross-domain request using the access token.

Please provide example JS code for a Data Fusion API call which will run server-side with Node.js?

data-fusionnode-jsajax
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvote
Accepted
76 16 21 29

Thanks to a consultation with EPAM and some advice from the Boston lab teams, here it is! Note this is just a demo which dumps the result to the console...

var express = require('express');
var app = express();
var q = require('q');
var https = require('https');


app.get('/sellsidebuyside', function (req, res) {
    var defer = q.defer();
    var referenceID = "1-4297777075";
    var myURL = "{instance hostname}";
    var myPath = "/datafusion/api/entity/search?limit=1000&dir=asc&parentUrisDirect=http%3A%2F%2Fpermid.org%2F" + referenceID + "&includePredicates=false&includeRelDir=false&parentPredicateFilters=30%7C%7C%7Chttp%3A%2F%2Frdf.thomsonreuters.com%2Frelationship%2FSellSideBuySideOrganization&filterType=and&includeHiddenFields=false";
    var access_token = "{your_token_here}";
    process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
    var options = {
        hostname: myURL,
        path: myPath,
        port: 443,
        method: 'GET',
        headers: {'Authorization': 'Bearer ' + access_token,
            'Content-Type': 'application/json',
            'Accept': 'application/json'}
    };
    var request = https.request(options, function(res) {
        console.log(res);
        res.setEncoding('utf8');
        var body = '';
        res.on('data', (chunk) => {
            body += chunk;
        });
        res.on('end', () => {
            console.log(body);
            defer.resolve(body);
        });
    });
    request.on('error', (e) => {
        console.log(e);
        defer.reject(e);
    });
    request.end();
    defer.promise.then((result) => {
        res.send(result);
    }, (error) => {
        res.send(error);
    });
});
var server = app.listen(8081, function () {
   var host = server.address().address;
   var port = server.address().port;
   console.log("Example app listening at http://%s:%s", host, port);
});
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
1.2k 8 11 8
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.