简体   繁体   中英

Using external modules within AppJs and nodejs

I've built a basic demo application using AppJS/NodeJS and edge.

Heres the relevant part of the the app.js config file - Basically it just references the external modules.

.......
window.on('ready', function(){
  console.log("Window Ready");
  window.process = process;
  window.module = module;
  window.dns = require('native-dns');
  window.edge = require('edge');
  window.fs = require('fs');
  window.frame.openDevTools();
  ..........

And heres the relevant javascript part of the main index.html page:

.......
   function myFunction1()
    {
        var question = dns.Question({
           name: 'www.google.com',
            type: 'A'
        });

    var req = dns.Request({
        question: question,
        server: { address: '8.8.8.8', port: 53, type: 'udp' },
        timeout: 1000
    });

    req.on('timeout', function () {
        console.log('Timeout in making request');
    });

    req.on('message', function (err, answer) {
        answer.answer.forEach(function (a) {
            console.log(a.address);
        });
    });

    req.on('end', function () {
        console.log('Finished processing request');
    });

    req.send();

}

 function myFunction2()
    {
  var helloWorld = edge.func('async (input) => { return ".NET Welcomes " + input.ToString(); }');

    helloWorld('JavaScript', function (error, result) {
        if (error) throw error;
        console.log(result);
    });
}

..........

If I call myFunction1() which uses another nodejs module (DNS lookup) it works perfectly. However if I call myFunction2() which uses edge I get the following error!

Uncaught TypeError: Property 'func' of object [object Object] is not a function  

I've spent hours on this and for cannot work out why this happening!

Have you tried running the same myFunction2 code inside app.js ie in nodejs? Maybe the func function does not exist on the edge object. Check the docs maybe you need to do something like window.edge = require('edge').Edge;

or something similar to get hold of the object that you think you have at the moment. You can also do console.log(window.edge) and see what it outputs (both in node and in browser running dev tools (F12)).

/Simon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM