• Raised here:


    Following the snippet from here:


    Testing on linux ( which has a side issue of the path not working due to it not being at the root of an SD card)

    echo index file > /tmp/www/index.htm


    function onPageRequest(req, res) {
      var a = url.parse(req.url, true);
      var f = E.openFile(a.pathname, "r");
      if (f !== undefined) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        f.pipe(res); // streams the file to the HTTP response
      } else {
        res.writeHead(404, {'Content-Type': 'text/plain'});
        res.end("404: Page "+a.pathname+" not found");

    And in browser:­htm

    at the debug point in the browser, when you hit c to continue we get:

    Uncaught Error: Expecting a function to call, got Number

    This does not occur for the not found case..

    Not sure what is causing this error or if the example needs to be updated?

  • Ok, that should be fixed now. The HTTP server was using close internally to decide if it should close the HTTP socket, and the pipe tried to call it when the file was empty.

    Just a note though: you didn't hit this on an official board did you, it's ESP32 and a linux build?

    I know you've done a bunch of work on Espruino's source, and this does apply to other boards so I don't mind fixing this for you. However people posting on the 'Official board' forum because they want me to fix their problems on non-official boards is exactly the kind of thing I'm trying to avoid.

    It's also why I moved the JS forum into the Official Boards section - because it was becoming a dumping ground for 'this doesn't work on my non-official board' posts.

  • Hi Gordon,
    Linux wasn't in the list of official boards - so I wasn't sure where to post. I'm happy to put it anywhere in the forum you want.

    I built the image since you did the patch for jumjum, and the error message above still occurs with the code above. I have no idea what is generating the error - it appears to be the return of the function call? I'm happy to look at it if you point me in the right direction.

  • No problem...

    Are you sure you're up to date (there should be a note in the ChangeLog about my fix) and make cleaned?

    I just compiled from git master here and ran your code as-is and it works perfectly.

    For debugging I'd say compile with DEBUG=1, run it under gdb and breakpoint jsExceptionHere - you can then look at the backtrace and see what really caused the error.

  • Thanks. I'll re-pull and check.

  • Sorry - I didn't have the latest...

    I see both now:
    Change name of socket close internal variable so it doesn't conflict with Pipe's close call
    Stop pipe from causing errors if fields of the requested names exist but aren't functions

    will merge and recheck.

  • I'll test also on the ESP8266 and report back, as it was definitely on that board that I first encountered the problem, as opposed to the ESP32.

  • Hi everyone, I've posted my results in GitHub. Thanks :)

  • Something appears to up with the pipe not ending as expected.


    I would be interesting if someone with an Espruino with an SD card could check this to see if the same issue exists.

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

E.openFile pipe - Uncaught Error: Expecting a function to call, got Number

Posted by Avatar for Wilberforce @Wilberforce