3
votes

1.) Question: I would like to know, how can I require and use a node module inside of an FOXX application.

i would like to use the NPM module named: node-json-rpc by nemopersona as a client. Probably also as a Server. But at least as a client to fetch data from somewhere else by RPC which is convenient.

It seems that FOXX does not like that specific line: As soon as I add this line the foxx app it throws an error.

var rpc = require('node-json-rpc');

I already added the NPM module (package) into various directories like:

/myfoxxappdirectory/node_modules
/usr/share/arangodb/js/common/modules
/usr/share/arangodb/js/node/node_modules

After I make changes I restart the Arangodb server. But it does not like modules. Also I think it does not like specificly that line:

// Create a server object with options
var serv = new rpc.Server(options);

And here you can see the full code of my FOXX App, which is not working.

(function () {

var rpc = require('node-json-rpc'); // FOXX throws error at that line

    //"use strict";

    var Controller = require("org/arangodb/foxx").Controller,
        Repository = require("org/arangodb/foxx").Repository,
        console = require("console"),
        arangodb = require("org/arangodb"),
        db = arangodb.db,
        actions = require("org/arangodb/actions"),
        //helloworld = require("./lib/a").text,
        controller = new Controller(applicationContext),
        central = new Repository(controller.collection("centraladdressdb"));

    // .............................................................................
    // Example: Route without parameters & simple text output with static text
    // .............................................................................

    controller.get('/hello', function (req, res) {
        res.set("Content-Type", "text/plain; charset=utf-8");
        res.body = "blabla it works!\n";
    });

}());

2.) Question: Are the FOXX commands asyncronous like in pure Nodejs? For example when we look at a command to find a ArangoDB Document in FOXX applications:

FOXX application code:

var accountdoc;
accountdoc = db.mysupercollection.document('rumpelstilzchen'); // find doc by _key

Obviously that is not an anonymous callback, am I right? It must be blocking. And does it block the server? This is what I am really wondering about, it must block the server. But could I also write ArangoDB database commands in FOXX-apps for I/O operations like a callback-style to avoid blocking? Its the big advantage of Nodejs and javascript to write non-blocking code. Is it possible to do that with FOXX too?

In Nodejs there is a javascript driver that does I/O to Arango in nonblocking style.

Then there are transactions in ArangoDB. There is also blocking. But for ACID transactions I think blocking itself is desirable. So there we would not need callbacks.

But in FOXX applications when accessing ArangoDB, why not there? Am I missing something??

Please help me if possible, and many thanks to you.

1

1 Answers

1
votes

(1) I think in order to get "node-json-rpc" module to work, support for a node.js compatible "http" modules is required. You should contact the google group https://groups.google.com/forum/?hl=de#!forum/arangodb

(2) ArangoDB is multi-threaded. It uses non-blocking I/O and workers to handle the requests. Therefore it does not block the server. If you write

var accountdoc;
accountdoc = db.mysupercollection.document('rumpelstilzchen'); // find doc by _key

in your Foxx application, this will get executed in a worker thread. It would be very difficult to make this call asynchronous, because unlike say the I/O communication there is no single event on which to react. Therefore it is much faster to use work threads instead.