• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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.

How can i pass query / parameters from URL to module

Scheduled Pinned Locked Moved Solved Development
12 Posts 3 Posters 2.1k Views 3 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.
  • A Offline
    AxLed Module Developer
    last edited by Mar 29, 2020, 11:59 AM

    Dear Developers,

    i want to redesign one of my modules. Therefore i need a hint how i can make the following possible.

    I want to pass a query / parameters from a module URL, f.e.

    http://IPofMM:8080/MMM-Navigate/index?value1=xxx&value2=yy
    

    to my module.
    What do i need?

    1. a page in my module folder index.js(?)
    2. code do read the values of querystring and do something with it.

    Maybe somebody can give me a tip how to do that or in which module it was already realized (except MMM-Remote-Control).

    Regards

    AxLED

    S 1 Reply Last reply Mar 29, 2020, 1:06 PM Reply Quote 0
    • S Away
      sdetweil @AxLed
      last edited by Mar 29, 2020, 1:06 PM

      @AxLed sorry. not quite sure what you mean…

      u want to pass the info from where (how is the user presenting this info) to where

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      1 Reply Last reply Reply Quote 0
      • A Offline
        AxLed Module Developer
        last edited by Mar 29, 2020, 1:10 PM

        @sdetweil
        User is presenting / entering datas by URL in his browser and module should be able to handle the datas.
        AxLED

        S 1 Reply Last reply Mar 29, 2020, 1:17 PM Reply Quote 0
        • S Away
          sdetweil @AxLed
          last edited by Mar 29, 2020, 1:17 PM

          @AxLed and did u setup an express path that invokes your node module function?

          the url parms should be there

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          1 Reply Last reply Reply Quote 0
          • A Offline
            AxLed Module Developer
            last edited by Mar 29, 2020, 1:28 PM

            @sdetweil
            No i didnt set any path so far, but express path seems to be the right search word and helps.
            If i understood the documentation right, express path has to be located in node_helper.js.
            But i still dont know how to move forward with this information.
            Are there some modules which are using express path, where i can try to figure out how it works?

            Greetings

            AxLED

            I

            S 1 Reply Last reply Mar 29, 2020, 1:32 PM Reply Quote 0
            • S Away
              sdetweil @AxLed
              last edited by Mar 29, 2020, 1:32 PM

              @AxLed MMM-ImagesPhotos uses it…

              the Module wants to use request, so issues a request to a local server (express path)
              which invokes the routine to get the list of photos//

              (instead of doing the sendSocketNotification to the node_module…)

              the express routine is like a cgi pgm invocation…

              from
              https://expressjs.com/en/guide/routing.html

              Route parameters

              the path can autoparse the url
              Route path: /users/:userId/books/:bookId
              Request URL: http://localhost:3000/users/34/books/8989
              req.params: { “userId”: “34”, “bookId”: “8989” }

              but the result, net, is the parms are in the req (uest) parameter object when the routine is called

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              1 Reply Last reply Reply Quote 0
              • A Offline
                AxLed Module Developer
                last edited by Mar 29, 2020, 4:12 PM

                @sdetweil
                Thanks for the response, it think now i have enough to read and i will try to get in up and running.

                I will give feedback within the next day.

                AxLED

                S 1 Reply Last reply Mar 29, 2020, 4:49 PM Reply Quote 0
                • S Away
                  sdetweil @AxLed
                  last edited by Mar 29, 2020, 4:49 PM

                  @AxLed cooll… standing by!

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  S 1 Reply Last reply Mar 30, 2020, 12:26 PM Reply Quote 0
                  • S Away
                    sdetweil @sdetweil
                    last edited by sdetweil Mar 30, 2020, 12:29 PM Mar 30, 2020, 12:26 PM

                    @AxLed so, it should be as simple as

                    const express = require('express')
                    
                    // setup server path handler, for http get request
                    this.expressApp.get("/MMM-Navigate",(req,res) =>{this.someFunction(req,res)});
                    
                    // handler dump request object
                    someFunction: function(request,response){
                         console.log("request data="+JSON.stringify(request))
                        response.send("Hello from MMM-Navigate");
                    }
                    

                    Sam

                    How to add modules

                    learning how to use browser developers window for css changes

                    1 Reply Last reply Reply Quote 0
                    • ? Offline
                      A Former User
                      last edited by A Former User Mar 30, 2020, 1:32 PM Mar 30, 2020, 1:31 PM

                      Here is another example of express;

                      const express = require('express')
                      const bodyParser = require('body-parser')
                      
                      // in your member function of node_helper.js, usually it will be `.start()`
                      
                      this.expressApp.use(bodyParser.json())
                      this.expressApp.use(bodyParser.urlencoded({extended: true}))
                      var process = (req, res) => {
                        var subpath = req.params.subpath
                        if (CHECK_SOME_CONDITION) {
                          var requestObj = {
                            path: subpath,
                            query: req.query,
                            post: req.body,
                            method: req.method,
                          }
                          this.sendSocketNotification("REQUEST_COMING", requestObj)
                          res.status(200).send({status: 200})
                        } else {
                          res.status(400).send({status: 400})
                        }
                      }
                      
                      this.expressApp.get("/YOUR_PATH/:subpath", process)
                      this.expressApp.post("/YOUR_PATH/:subpath", process) // when you need POST method.
                      
                      1 Reply Last reply Reply Quote 1
                      • 1
                      • 2
                      • 1 / 2
                      1 / 2
                      • First post
                        1/12
                        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