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

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

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!

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

Related

Writing WebSocket client with TypeScript running both on browser and Node.JS

I am writing a typescript code that would run in a web-browser and would be tested with Node.JS.
My client code looks like below.
import * as WebSocket from 'ws';
export class SomeClient {
constructor(url) {
this.ws = new WebSocket(url);
}
send(data: any) {
this.ws.send(data);
}
}
I had no problem in writing a unit test code using mocha/chai.
However, trying to bundle this code, browserify includes all the 'ws' node module and the size of the output file is almost 100kb. If I remove the import 'ws' statement, the bundle file size shrinks less than 1kb. But, in this case, the Node.JS test complains with 'WebSocket is not defined' error.
I think, this is because WebSocket is natively supported in web browsers but not supported in Node.JS and the external 'ws' module is required to run properly.
How can I make a bundle with the minimum size for web browsers yet can use in Node.JS???
Try isomorphic-ws:
npm i isomorphic-ws -s
or universal-websocket-client:
npm install --save universal-websocket-client
I struggled with the same problem, best solution I could find was to use isomorphic-ws create a decs.d.ts in my typescript rootDir with the following content
declare module "isomorphic-ws";
and then use it inside typescript like that:
import { IsoWebSocket } from "isomorphic-ws";
var ws = new IsoWebSocket("wss://echo.websocket.org") as WebSocket;

How to use npm packages in laravel

I am trying to use a npm package in my laravel5 app, but I am not aware with node and require, can someone please help me to understand that how can I use the npm packages.
I am trying to use the following package:
https://www.npmjs.com/package/node-movie
Here is my code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script data-main="{!! url() !!}/vendor/lib/require.js" src="{!! url() !!}/vendor/lib/require.js"></script>
<script type="text/javascript">
var imdb = require('imdb-api');
var movie;
imdb.getReq({ name: 'The Toxic Avenger' }, function(err, things) {
movie = things;
});
console.log(movie);
</script>
I am getting the following error in console:
Uncaught Error: Module name "imdb-api" has not been loaded yet for context: _. Use require([])
First thing:
You're trying to use node on the clientside; this means you don't know what node is.
To get started with it:
How do I get started with Node.js
Second:
You should understand what asynchronous means in javascript and node and how to work with it. Your console.log(movie) will log undefined every time even if your code works.
You can search for articles pertaining to async in node; here's an example article: https://www.codementor.io/nodejs/tutorial/manage-async-nodejs-callback-example-code
Third:
To use node with laravel (after you learn how to use node) you can start up a node server that works as a REST API to provide your laravel app with what you require from node packages (you 'request' the information from the node server).
Easier alternative:
Find a pure javascript implementation for that npm package (i.e. search for a solution that doesn't involve node).

using require() in karma tests

I'm using Karma 0.12.28, Karma-Requirejs 0.2.2 (and Karam-jasmine) to run tests.
Config is almost identical to one from docs (test files in requirejs deps and window.__ karma __.start as callback).
Everything works just fine for basic tests. The problem starts when I use require() instead of define() or try to change context. Basically something like this:
var ctx = require.config({
context: 'my-context'
});
ctx(['dep1'], function(){
//....
});
The problem is dep1 fails to load. In DevTools I can see <script/> is created and I can see request in network tab with proper URL but status is canceled. I can open this URL using context menu so I'm sure it is correct but the question remains - why can't I use require() in karma tests?

Problems directly using PhantomJS in node.js

I'm attempting to use PhantomJS, and I've installed it via NPM.
I can't seem to run any of the of the examples, in fact I can't even run:
var page = require('webpage').create();
I get the error:
Error: Cannot find module 'webpage'
Is there anything i'm missing? I'm using a few other modules that I've installed via NPM in the same directory with no issues
PhantomJS is not for Node.js. You are likely running the examples through node binary.
Read the Getting Started documentation carefully and you'll see that every single PhantomJS example need to be invoked like:
phantomjs hello.js
Note that there is a bridge between Node.js and PhantomJS. In that case, you need to follow the given examples for that particular bridge (there are a few different ones).
You can use something like this:
var page = new WebPage();
Example of code :
var page = new WebPage();
page.open('http://example.com', function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render('example.png');
}
phantom.exit();
});

Node.js + Express + Jade :: render( ... ) times out with Node v.0.8.12

I've been using Node.js + Express + Jade for a long time.
Since upgrading to node 0.8.12, the render( ... ) command just times out on my production server.
In this example, "1" is printed to the log, but "2" is not, and the page times out. Of course, I properly have a file called views/test.jade. And, again, this works fine on my test server with the same code...
console.log('1');
res.render('test');
console.log('2');
So I downgraded Node.js back to 0.6.18 (what I was using before) and the code works fine again. But I need the newer node version... the one major difference that I can think of is that 0.6.18 was installed on CentOS via YUM, but I had to make v0.8.12 myself because I could not find an appropriate package.
Here's what I've tried:
Upgrading express (#3.0.0rc5) and jade (#0.26.3)
Using a callback in the render() func (it is never called)
Explicitly setting the views/ directory via app.set('views', absolute_path);
Using an invalid template name intentionally in attempts to get an error. Still, nothing (no callback fired, no execution)
Crying
Ideas?
Make sure that jade is correctly installed. If the render call is timing out, it most likely means that an error is being thrown that you are not handling. You can check for errors being thrown like this:
try {
res.render('test');
}
catch (e) {
console.log(e);
}
Since render doesn't depend on much, I'd guess that the jade module isn't installed correctly. Make sure that you see jade in your node_modules\express folder. If you don't, try re-installing:
npm install express

Resources