Loading modules from a webserver

Posted on
  • I have my own nodejs webserver and want to load my modules from this server. When I enter the following code on the right side of the Web IDE everything works hwever I get error messages like "Unexpected reserved word in http://192.168.88.117:8081/Wireless.js":­

    Transmit = require("http://192.168.88.117:8081/tran­smitClass.js");
    Wireless = require("http://192.168.88.117:8081/Wire­less.js");
    Switch = require("http://192.168.88.117:8081/swit­chClass.js");
    
    oWl = new Wireless('myAp', '192.168.88.117', new Switch());
    oWl.init();
    

    when I enter the command on the left side, the file is not found:

    Transmit = require("http://192.168.88.117:8081/tran­smitClass.js")
    Uncaught Error: Module http://192.168.88.117:8081/transmitClass­.js not found
     at line 1 col 65
    ....117:8081/transmitClass.js")
    

    The same happens, when I require such a module within the module:

      doTransmit() {
        console.log('Call Transmit');
        var url = 'http://'+this.wServer+':8081/transmitCl­ass.js';
        Transmit = require(url);
        this.oTr = new Transmit(this.wServer, this.mThing);
        this.oTr.init();
      }
    

    I do not understand this behaviour. Could somebody explain this to me?

    Thanks for all,
    Thomas

  • Modules in the right, are resolved using your computer internet connection, and loaded with your code which is sent over serial/bluetooth to espruino device. In the left, you're on the espruino board itself and modules are not loaded. Indeed you've no guarantee of internet on some boards. Similarly, I woudn't expect you could require and resolve http addressed modules at runtime as you appear to be doing. "Unexpected reserved word" I don't know about.

  • Unexpected reserved word in ....

    Could be a syntax error in the module code.
    Copy & paste the code into right site for a syntax check.

  • Just to add to this: The IDE scans your code on the right hand side for mentions of require(url) - however it isn't smart enough to dynamically load module URLs that are specified in a variable.

    Espruino itself can't easily load modules via require because an HTTP request to get them would take a certain amount of time. Instead, if you want to load the module yourself do something like:

    function loadModule(moduleName, url, callback) {
      require("http").get(url, function(res) {
        var contents = "";
        res.on('data', function(data) { contents += data; });
        res.on('close', function() {Modules.addCached(moduleName, contents); callback(); });
      });
    }
    
    loadModule("transmitClass", "http://192.168.88.117:8081/transmitClas­s.js", functtion() {
      Transmit = require("transmitClass");
    });
    
  • Tue 2019.01.15

    Slick!

    Add to Tips-and-Tricks?

  • Thanks for this tip! This brought me to the Idea to store the modules to the flash using Storage. This would allow to load the modules later also offline.
    My application has multiple modules. This approch would allow me to work on a smaller module without the need to load all the time all modules.

    However I came over a problem with the storage module, i'll put this in another thread.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Loading modules from a webserver

Posted by Avatar for fanThomas @fanThomas

Actions