Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a library that calls xmlhttprequest in node.js?

Tags:

node.js

odata

I would like to run tests of OData interfaces from node.js, using the data.js library. Unfortunately, data.js is intended for use in browsers and uses XMLHttpRequest calls. node.js cannot handle such calls because, I believe, they are implemented in the browser, not in JavaScript. Is there a module that will let me use data.js in node.js?

The usual solutions for XMLHttpRequest are OK when you can call them in your own code, but here I don't want to change data.js, so those options are not open.

Here is a sample of what goes wrong:

var odata = require("./datajs-1.1.0.js");
try{
    odata.OData.read( 
        "http://services.odata.org/Northwind/Northwind.svc/Categories" 
    );
}
catch(err){
    console.log ("Exception in index.js - " + err.name + ": " + err.message);
}

Running node.js index.js:

Exception in index.js - undefined: XMLHttpRequest not supported
like image 358
Tom Slee Avatar asked Oct 22 '22 09:10

Tom Slee


2 Answers

The exception may be a result of requiring the datajs library with a file system literal ("./") instead of as a node_module (which would be more along the lines of require("datajs")). The latter normally requires use of npm (it stands for node packaged modules), and you would want to run the following command in your project directory:

npm install datajs

The reference page for the datajs library can be found at https://npmjs.org/package/datajs. Good Luck!

like image 98
Sandile Avatar answered Oct 27 '22 11:10

Sandile


There is a module for that, npm install xmlhttprequest (or similar). That module has issues with escaping... Or you can use jaydata which just works

like image 26
Gabor Dolla Avatar answered Oct 27 '22 09:10

Gabor Dolla