Read the statement by Michael Teeuw here.
MM2 can get POST API?
-
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..'); } }); }); });
-
@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 ); }); },
-
@hikano765
usethis.expressApp
in node_helper.js
Of course, you can use your ownexpress
orfastify
whatever by importing that node module.