简体   繁体   English

在AppJ和Node.js中使用外部模块

[英]Using external modules within AppJs and nodejs

I've built a basic demo application using AppJS/NodeJS and edge. 我已经使用AppJS / NodeJS和edge构建了一个基本的演示应用程序。

Heres the relevant part of the the app.js config file - Basically it just references the external modules. 这是app.js配置文件的相关部分-基本上,它仅引用外部模块。

.......
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: 以下是index.html主页中相关的javascript部分:

.......
   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. 如果我调用使用另一个nodejs模块(DNS查找)的myFunction1(),则它可以正常工作。 However if I call myFunction2() which uses edge I get the following error! 但是,如果我调用使用edge的myFunction2(),则会出现以下错误!

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? 您是否尝试过在app.js中(即在nodejs中)运行相同的myFunction2代码? Maybe the func function does not exist on the edge object. 可能func函数在边缘对象上不存在。 Check the docs maybe you need to do something like window.edge = require('edge').Edge; 检查文档,也许您需要执行诸如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)). 您还可以执行console.log(window.edge)并查看其输出(在节点和运行开发工具(F12)的浏览器中)。

/Simon /西蒙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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