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.

    MM2 can get POST API?

    Scheduled Pinned Locked Moved Development
    3 Posts 3 Posters 714 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.
    • H Offline
      hikano765
      last edited by

      I am developing a module of MM.
      By the way, I want to control one of these modules by an external client, and I want to do that control method by REST API method.
      For example, if an external client sends a POST API to MM2, the corresponding message is displayed on the MM2 screen.
      I don’t know because I haven’t read the MM2 code in detail, but as far as I’ve checked, the server also automatically turns on when running MM2, so I want to process REST API communication on that server as well, but which file should I modify and put a phrase related to REST API communication in order that?

      server.js?
      server_function.js?

      Examples of API codes that I want to put in are these.
      The API code is an API that stores the message as a txt file when a message is transmitted in a POST method.

      app.post('/api/saveMessage', (req, res) => {
          const message = req.body.message;
      
          if (!message) {
              res.status(400).send('Message not provided.');
              return;
          }
      
          fs.unlink(`appid.txt`, (err) => {
              fs.appendFile(`appid.txt`, message + '\n', (err) => {
                  if (err) {
                      console.error('Error saving message:', err);
                      res.status(500).send('Error saving message');
                  } else {
                      res.send('The message was saved successfully..');
                  }
              });
          });
      });
      
      S M 2 Replies Last reply Reply Quote 0
      • S Offline
        sdetweil @hikano765
        last edited by sdetweil

        @hikano765 you use express lib to set up a server.

        you can look at the node_helper in my mmm-config module, as I provide an API.

        https://github.com/sdetweil/MMM-Config

        I redirect , but u can do whatever

          // add express routes to allow invoking the form
          extraRoutes: function () {
            this.expressApp.get("/modules/MMM-Config/review", (req, res) => {
              // redirect to config form
              res.redirect(
                //this.config.url +
                "/modules/" + this.name + "/config.html?port=" + socket_io_port
              );
            });
          },
        

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        1 Reply Last reply Reply Quote 0
        • M Offline
          MMRIZE @hikano765
          last edited by

          @hikano765
          use this.expressApp in node_helper.js
          Of course, you can use your own express or fastify whatever by importing that node module.

          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