• 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.

MM2 can get POST API?

Scheduled Pinned Locked Moved Development
3 Posts 3 Posters 566 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 Nov 4, 2023, 4:54 AM

    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 Nov 4, 2023, 9:15 PM Reply Quote 0
    • S Offline
      sdetweil @hikano765
      last edited by sdetweil Nov 4, 2023, 9:42 PM Nov 4, 2023, 9:15 PM

      @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 Nov 4, 2023, 9:39 PM

        @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
        • 1 / 1
        1 / 1
        • First post
          1/3
          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