MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    readFileSync

    Scheduled Pinned Locked Moved Development
    17 Posts 3 Posters 12.4k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      mattlugar
      last edited by

      I’ll preface this by saying I’m fairly new at nodejs, so maybe I’m doing something totally stupid. I’ve been beating my head on this for a while now… Not making any sense to me whatsoever, googling like crazy and still totally stuck.

      I have bigger plans (reading a locally updated JSON file and displaying frequently updated data to the screen), but as nothing seemed to work I kept paring and paring the project back to where I’m now simply trying to display static contents of a local file to the screen. I altered helloworld to try to do this simple task. Note that the file ‘/home/pi/test.txt’ is simply the word “test”, and has permissions set to where anyone can read it. I’m also loading other modules just so I know something’s working.

      The following code locks up the mirror, displaying simply a black screen. Note that I am not even attempting to display the file at this point, I’m simply trying to load it into the variable ‘file’.

      Module.register("helloworld",{
      
              getDom: function() {
                      var wrapper = document.createElement("div");
                      var file = require('fs').readFileSync('/home/pi/test.txt', 'utf8');
                      wrapper.innerHTML = "Hello";
                      return wrapper;
              }
      });
      

      I can tell that the issue is my loading line because I can comment it out thusly and all the modules display, including the “Hello” text:

      Module.register("helloworld",{
      
              getDom: function() {
                      var wrapper = document.createElement("div");
      //              var file = require('fs').readFileSync('/home/pi/test.txt', 'utf8');
                      wrapper.innerHTML = "Hello";
                      return wrapper;
              }
      });
      

      OK, so maybe there’s a problem with my loading statement. Easy to check, copy/past the line into a js file and run it:

      test.js:

      var file = require('fs').readFileSync('/home/pi/test.txt', 'utf8');
      console.log(file);
      

      And here’s what happens:

      pi@lugarpi:~ $ nodejs test.js
      test
      

      Works perfectly fine in nodejs but if I put that line of code into the module it locks up the mirror. Am I missing something here? Also, I’m launching mirror through pm2. I understood any errors should show up in ~/.pm2/logs but the error log doesn’t show anything.

      1 Reply Last reply Reply Quote 0
      • MichMichM Offline
        MichMich
        last edited by

        What does it lockup? The fronted? Or the backend?

        If it’s just the frontend, check your webinspector/console to see what’s going on.

        M 1 Reply Last reply Reply Quote 0
        • M Offline
          mattlugar @MichMich
          last edited by

          That’s where my lack of knowledge on how to debug in this new environment was hurting me. Was able to figure out a better way to debug and now have something to work with.

          Uncaught ReferenceError: require is not defined
          

          Off to roll up my sleeves and do some mad google-fu.

          Thank you! :)

          1 Reply Last reply Reply Quote 0
          • KirAsh4K Offline
            KirAsh4 Moderator
            last edited by

            That’s because require isn’t part of JavaScript. It’s part of node.js to load modules. There are potentially other ways of doing what you’re trying to do. You may want to check out this post on Stack Overflow.

            A Life? Cool! Where can I download one of those from?

            1 Reply Last reply Reply Quote 0
            • M Offline
              mattlugar
              last edited by

              Yeah, I went about it differently than I wanted to. I ended up porting everything to an apache directory so it serves itself as a webapp. Had a few bugs to work out (it was locking up if the json was in the middle of writing when it was retrieved) but I have it working and bug free now best I can tell :)

              1 Reply Last reply Reply Quote 0
              • KirAsh4K Offline
                KirAsh4 Moderator
                last edited by

                I suppose that’s one way to do that … doesn’t make it very portable or sharable to others. It’s no longer contained within only MM and Node.js … But, if it’s working for you and does what you want, that’s the only thing that matters.

                A Life? Cool! Where can I download one of those from?

                M 1 Reply Last reply Reply Quote 0
                • MichMichM Offline
                  MichMich
                  last edited by

                  Just so you know, the node_helper can serve files as well: https://github.com/MichMich/MagicMirror/tree/master/modules#thisexpressapp

                  1 Reply Last reply Reply Quote 0
                  • KirAsh4K Offline
                    KirAsh4 Moderator
                    last edited by

                    I saw that when I was fiddling with some other stuff, but never actually tested it. Is that path relative to the actual module location? Or MM’s installation? Or the user that’s running MM? So if '/foo/bar/baz' is defined, where does it start?

                    /foo/bar/baz? (OH PLEASE SAY NO, for the love of all that is sacred, tell me Node.js does NOT allow system reads like that!)
                    $HOME/foo/bar/baz ?
                    $HOME/MagicMirror/foo/bar/baz?
                    $HOME/MagicMirror/modules/foo/bar/baz?
                    $HOME/MagicMirror/modules/<MODULE>/foo/bar/baz?
                    

                    A Life? Cool! Where can I download one of those from?

                    1 Reply Last reply Reply Quote 0
                    • MichMichM Offline
                      MichMich
                      last edited by

                      If you are referring to this snippet:

                      start: function() {
                          this.expressApp.get('/foobar', function (req, res) {
                              res.send('GET request to /foobar');
                          });
                      }
                      

                      The /foobar is referring to the url: http://localhost:8080/foobar

                      1 Reply Last reply Reply Quote 0
                      • KirAsh4K Offline
                        KirAsh4 Moderator
                        last edited by

                        Ah, so it’s still an HTTP call then, not a simple fileread from disk.

                        A Life? Cool! Where can I download one of those from?

                        1 Reply Last reply Reply Quote 0
                        • MichMichM Offline
                          MichMich
                          last edited by

                          Exactly.

                          1 Reply Last reply Reply Quote 0
                          • KirAsh4K Offline
                            KirAsh4 Moderator
                            last edited by

                            So then the question is still, where is that '/foobar' ? What’s that in relation to? The MM install? The module’s folder?

                            A Life? Cool! Where can I download one of those from?

                            MichMichM 1 Reply Last reply Reply Quote 0
                            • MichMichM Offline
                              MichMich @KirAsh4
                              last edited by MichMich

                              @KirAsh4 it hasnt got anything to do with a file. It’s just a request, if you want to respond with the contents of a file, you need to do that yourself or use the following method if you want to feed a full folder:

                              this.expressApp.use("/foobar" + this.name, this.expressApp.static(this.path + "/foobar"));
                              
                              1 Reply Last reply Reply Quote 0
                              • KirAsh4K Offline
                                KirAsh4 Moderator
                                last edited by

                                No, it doesn’t have anything to do with the contents. It has to do with where that file resides on the physical system. For example, a request to a default Apache installation will look for a file in '/var/www/html/' or '$USER/public_html' or whatever the system admin has configured it as. In this case, this is a specific request through the Node.js system, being called by MM, so where does it look for that file? Within the user’s folder? Within MM’s installation folder? Within the actual module’s folder? Or does Node.js allow access to the entire file system? So that if I do a call such as:

                                this.expressAp.use("/etc/passwd")
                                

                                will I actually get the contents of the system’s '/etc/passwd' file, or is going to try to read '/path/to/MM-install/etc/passwd' or some other path?

                                A Life? Cool! Where can I download one of those from?

                                1 Reply Last reply Reply Quote 0
                                • MichMichM Offline
                                  MichMich
                                  last edited by

                                  I’ve got the feeling we’re talking about something different.

                                  The example:

                                  this.expressApp.get('/foobar', function (req, res) {
                                          res.send('GET request to /foobar');
                                  })
                                  

                                  does not serve any file. So talking about file location doesn’t make any sense in this case. In this example It just serves the text string GET request to /foobar

                                  In the second example:

                                  this.expressApp.use("/foobar" + this.name, this.expressApp.static(this.path + "/foobar"));
                                  

                                  only the express.static(root, [options]) part refers to a file or folder. The root argument refers to the root directory from which the static assets are to be served.

                                  More info about express.static can be found here: http://expressjs.com/en/api.html#express.static

                                  KirAsh4K 1 Reply Last reply Reply Quote 0
                                  • KirAsh4K Offline
                                    KirAsh4 Moderator @MichMich
                                    last edited by

                                    @MichMich said in readFileSync:

                                    The root argument refers to the root directory from which the static assets are to be served.

                                    That’s part of the answer I was looking for. Specifically, 'from which the static assets are to be served'. Now to figure out where those are, or where they are allowed to be rather. Basically I’m looking at it from a security stand point. Where is this thing going to allow a user to get to, to fetch a file.

                                    A Life? Cool! Where can I download one of those from?

                                    1 Reply Last reply Reply Quote 0
                                    • M Offline
                                      mattlugar @KirAsh4
                                      last edited by

                                      @KirAsh4 Yeah, it is working for me as I want it. I figured it wouldn’t be overly useful to others anyhow since there was a lot of back-end work to do (namely setting up json streams that match the module code from the weather station, uploading to an appropriate location).

                                      Screenshot of my current display is below. It updates every 5 seconds using data from my local Davis weather station running Cumulus software. The rain information (total daily rain on top left, current rain rate at bottom left) only display if there has been rain today or it is currently raining respectively. It will also display winds like “12 G23” if the winds hit gust criteria (over 18MPH gusts and more than 10MPH over the wind speed). It is pretty trivial to put any information you can get into the json file to display.

                                      I’d be happy to try to properly modularize this if there is some interest, there’s just so many options and variations of stations/json exports/etc that I’m not sure I’d know where to start.

                                      alt text

                                      1 Reply Last reply Reply Quote 0

                                      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                      With your input, this post could be even better 💗

                                      Register Login
                                      • 1 / 1
                                      • First post
                                        Last post
                                      Enjoying MagicMirror? Please consider a donation!
                                      MagicMirror created by Michael Teeuw.
                                      Forum managed by Sam, technical setup by Karsten.
                                      This forum is using NodeBB as its core | Contributors
                                      Contact | Privacy Policy